Tag Archives: bash

Quick Images with TikZ

How do you create high-quality technical images for documents, your website or posts on Stack Exchange? I have used tools in an ad-hoc manner for a while and have become frustrated lately. Once you have used TikZ1 with \(\LaTeX\) most other tools feel inferior. The only problem is: TikZ is a \(\LaTeX\) package and can not be used on its own. So how to convert TikZ to say PNG comfortably?

\(\TeX\)-guru Martin Scharrer comes to the rescue: he wrote the package standalone for exactly this use case. Based on his explanation on tex.SE I built a small bash script that does all the repetitive work for you. With one simple command, this

%p% \usetikzlibrary{arrows,automata,positioning}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,auto]
  \node[state,initial]    (q_0)                {$q_0$};
  \node[state,accepting]  (q_1) [right of=q_0] {$q_1$};

  \path[->] (q_0) edge [bend left]  node {$a$} (q_1)
            (q_1) edge [bend left]  node {$b$} (q_0);
\end{tikzpicture}

becomes this in a matter of seconds:
Example

GPL3

Get tikz2png on GitHub and enjoy!


  1. Check out the awesome gallery of examples and the comprehensive manual.

Keep Your Display Active

Do you have problems with your display going idle on you all the time, too? Here is the solution: trick it into believing you were doing something useful by doing something stupid.

#!/bin/bash
while [ true ]; do xsendkeys Shift_L; sleep 30; done

Download Photos From Canon EOS

My Canon EOS 450D is not recognised as mass storage by my system when connected via USB. In order to get photos from camera to PC, you can use gphoto2:

#!/bin/bash
echo -n "[pullphotos] Detecting camera ...";
gphoto2 --auto-detect > /dev/null;
sleep 1s;
echo " Done";

echo -n "[pullphotos] Pulling photos ...";
gphoto2 -P &> /dev/null;
sleep 1s;
echo " Done";

echo -n "[pullphotos] Rotating photos ...";
jhead -autorot * &> /dev/null;
echo " Done";

This script copies all images currently stored on the camera into the current folder and rotates them if necessary1. The necessary programs gphoto2 and jhead are available as packages with the same name from the Ubuntu repositories. Note that you have to empty your camera’s storage manually afterwards.

Furthermore, note that gphoto2 is a very powerful tool. It allows you to configure and control many aspects of your camera via USB, including remote capture—something you might want to employ?


  1. Employing some sensor trickery, modern cameras annotate image files with information about picture orientation.

Cover Art With conky And mpd

After one year of using mpd on my Fluxbox machine I have started to sift through my music collection, fixing tags and adding album covers. Since there is no need for having album art if you do not show it, I set out to make conky show the images on my disk. I was lucky to find script in the depth of some forum (reference lost) that inspired the following piece of bash code.

#!/bin/bash
IFS=$'\t' mpd_array=( $(MPD_HOST=localhost MPD_PORT=6600 \
          mpc --format "\t%artist%\t%album%\t%file%\t") );

filename="conky_cover.png";
placeholder="/path/to/placeholder.png"

if [[ ! -f /tmp/"${mpd_array[1]}".album ]] ; then
  rm -f /tmp/*.album &> /dev/null;
  >/tmp/"${mpd_array[1]}".album;

  album=`dirname "/path/to/music/${mpd_array[2]}"`;
  cover=$album"/"`ls $album | egrep "jpeg|jpg|png|gif" | head -n 1`;

  if [[ ! -f $cover ]]; then
    cp $placeholder /tmp/$filename;
  else
    convert $cover -resize "130>x" -alpha On -channel A \
                   -evaluate set 25% /tmp/$filename;
  fi
fi

When this script is run, the currently playing album’s artist and name are retrieved and the corresponding image — or a dummy — is copied to /tmp for conky to pick up. Note that always the first image in the album’s folder is chosen and then manipulated with imagemagick; you might have to do a little coding yourself if you store your music or covers differently. Note that all time consuming stuff is done only once per album run.

On the conky side, life is easy now. Just add something like this to your .conkyrc:

${if_running mpd}
MPD: ${alignr}${mpd_status}${if_mpd_playing}
 ${mpd_artist 25}
 ${mpd_album 25}
 ${mpd_title 25}
 ${mpd_bar 3,130}
 ${exec ~/bin/conky_mpd_art}${image /tmp/conky_cover.png -p 3,772 -s 130x130 -n}
${endif}
${endif}

You have to adapt the visual thingies to your liking, of course.

Tastaturbelegung wechseln mit Fluxbox

Wer eine Fremdsprache lernt oder einfach lieber mit amerikanischer Tastenbelegung programmiert, muss regelmäßig sein Tastaturlayout verstellen. Während GNOME ein entsprechendes Applet für das Panel mitbringt, sieht es bei Fluxbox erstmal mau aus. Ich habe mir ein Skript geschrieben, das durch mehrere Tastaturlayouts rotiert und visuelles Feedback dafür liefert. Ist keines der angegebenen Layouts gesetzt, wird das erste gewählt.

#!/bin/bash
layouts=(
 "de"    "German"    "de.png"
 "se"    "Swedish"    "se.png"
);
errimg="error.png";

current=`
 setxkbmap -print |
 grep xkb_symbols |
 awk '{print $4}' |
 awk -F"+" '{print $2}'`;
next=0;

for (( i = 0 ; i < (${#layouts[@]}/3) ; i++ )) do
  if [ $current = ${layouts[$i*3]} ]; then
    next=($i+1)%${#layouts[@]/3};
  fi
done

return=`setxkbmap ${layouts[3*$next]} 2>&1`;

if [ ${#return} -gt 0 ]; then
  notify-send -i ${errimg}\
                      "Failed Setting Layout ${layouts[3*$next]}"\
                      "$return";
else
  notify-send -i ${layouts[3*$next + 2]}\
                     "Switched Keyboard Layout"\
                     "${layouts[3*$next + 1]}";
fi

Um weitere Layouts hinzuzufügen, müssen in das Array layouts je drei Strings eingefügt werden; der erste bezeichnet das Layout, der zweite ist der Ausgabestring und der dritte ist der Pfad zum entsprechenden Icon. Will man etwa auf das Bild verzichten, unbedingt einen leeren String angeben, da sonst die Indexberechnung schief läuft. Eine Auswahl an Layoutbezeichnern kann zum Beispiel hier gefunden werden.

Zu schwedischem Layout und zurück

Als Quelle muss ich diesen Post erwähnen. Eine kommentierte Fassung gibt es hier. Natürlich müssen die Pfade zu Bildern angepasst und die genannten Pakete installiert werden. Das Skript sollte grundsätzlich mit allen Window Managern funktionieren. Um es (unter Fluxbox) ohne Terminal oder fbrun ausführen zu können, kann man es natürlich mittels an ein Tastaturkürzel binden, in dem man in  ~/.fluxbox/keys etwa diese Zeile einfügt:

Mod1 F3 :execCommand ~/bin/switchkbl

Nachtrag: Die nötigen Pakete unter Ubuntu sind: setxkbmap, awk, libnotify-bin