Mit Imagemagick Bilder automatisiert bearbeiten

Jeder hat sicherlich schon einmal mit einem umfangreichem Grafikprogramm wie beispielsweise GIMP oder Photoshop gearbeitet. Sicherlich kann man mit solchen Programmen so gut wie jedes gewünschte Ergebnis erzielen, was sicherlich daran liegt, dass diese Programme weniger für den privaten, sondern eher für den professionellen Anwender entwickelt wurden. Für viele Anwendungen sind GIMP, Photoshop & Co einfach zu komplex, so dass die Devise lautet: "Weniger ist manchmal mehr!"

Image Magick Logo

Es macht nicht wirklich Sinn, für kleinere Aufgaben ein solch gewaltiges Tool zu benutzen und wenn diese Aufgaben auch noch automatisiert werden, so wird alles ganz schnell lästig. GIMP und Photoshop besitzen zwar auch Funktionen, um Vorgänge und Aufgaben zu automatisieren, doch benötigt man hier teilweise komplexe Scripte.

Und hier kommt die Grafikbibliothek "ImageMagick" ins Spiel. ImageMagick wird bei zahlreichen Web Content Management Systemen wie beispielsweise Typo3 eingesetzt, um Grafikfunktionen bei der Administration einer Webseite einzusetzen. Das Online-Auktions-Haus Ebay setzt zwar nicht auf Typo3, aber jeder kennt sicherlich die automatisierte Funktion, wenn man Bilder für den Verkauf hochlädt. Die Größe des Bildes wird automatisch geändert und ein kleines Logo auf das Bild gesetzt. Diese Tätigkeit erledigen mit Sicherheit nicht tausende von Mitarbeitern, sondern eine Grafikbibliothek wie ImageMagick.

Anwendungsbeispiele

Fall 1: Urlaubsbilder

Man stelle sich vor, man habe im letzten zwei-wöchigem Teneriffa-Urlaub die Digitalkamera intensiv genutzt und nun stellt man fest, dass bei knapp 30 Bildern die Bilder zu dunkel sind. Die Helligkeit muss höher eingestellt werden. Wenn man jetzt 30 Bilder einzelnd bearbeitet, erhält man erstens unterschiedliche Ergebnisse und zweitens nimmt dies sicherlich 30 bis 45 Minuten in Anspruch. Mit ImageMagick dauert das ganze bei einem aktuellen Rechner weniger als eine Minute.

Fall 2: Urlaubsbilder für Internet optimieren

Nun möchte man die Bilder gerne online verfügbar machen, damit Freunde und Bekannte die Bilder jederzeit und überall betrachten können. Da im Urlaub eine hochauflösende Digitalkamera genutzt wurde, ist jedes einzelne Bild knapp 1 MB groß. Um die Bilder für das Web zu optimieren, müssen wir die Auflösung und die Qualität ändern. Viel Spaß für die Anwender, die jedes einzelne Bild manuell bearbeiten, denn bei ca. 150 Urlaubsbildern wird dies sicherlich ein bis zwei Stunden in Anspruch nehmen. Mit ImageMagick kein Problem, denn mit einer Kommandozeile wird diese Aufgabe automatisiert in schätzungsweise 2-3 Minuten erledigt.

Fall 3: Copyright

Damit Karl Lagerfeld nicht auf die Idee kommt und ungefragt unsere Bilder für die nächste Werbekampagne nutzt, möchten wir, dass auf jedem Bild ein Schriftzug mit unserem Namen bzw. dem Datum oder Ort gesetzt wird. Diese Funktion wird auch oft von Webadministratoren genutzt, um Bilderklau vorzubeugen. Auch dies ist mit ImageMagick möglich.

Anleitung

ImageMagick bietet eine riesen Fülle von Funktionen. Wir gehen aber nur auf die wesentlichen Funktionen ein, so dass jeder, der weitere Funktionen ausprobieren möchte, einfach mal in die Manpage schauen sollte. Damit wir alle Bilder in einem Ordner bearbeiten können, müssen wir eine Schleife (Begriff aus der Programmierung) konstruieren. Dieses machen wir einmalig und die Schleife bildet sozusagen unseren Rahmen:

1. Schritt: Schleife

for i in *.jpg;
do convert $i `basename $i .jpg`.jpg;
done

Diesen Teil muss man nicht verstehen. Wichtig für uns ist, was nach "convert" folgt, denn das ist das eigentliche Programm. Wer möchte, kann einfach mal manuell mit dem Befehl "convert -scale 600 bild.jpg" ein Bild skalieren. Mit dem oben genannten Script wird nicht nur ein Bild, sondern alle jpg-Dateien im jeweiligen Verzeichnis bearbeitet. Aus diesem Grund das auf den ersten Blick kompliziert erscheinende Konstrukt.

Als nächstes möchten wir, dass die veränderten Bilddateien mit zusätzlichem Text umgenannt werden, damit wir diese im Verzeichnis schnell wieder finden und wir die Original-Dateien nicht ändern. Beispiel: Aus pic001.jpg soll urlaub_ten06_pic001.jpg werden.

2. Schritt: Dateinamen umbenennen

for i in *.jpg;
do convert $i urlaub_ten06_`basename $i .jpg`.jpg;
done

Jetzt können wir beginnen, für das Programm convert die Optionen festzulegen. Als leichtes Beispiel wollen wir vorerst nur die Bildgröße ändern. Unsere Bilder sollen auf 600 Pixel breite skaliert werden:

Bilder skalieren

for i in *.jpg;
do convert -scale 600 $i urlaub_ten06_`basename $i .jpg`.jpg;
done

Mit diesem kurzen Kommando erreichen wir, dass alle Bilder auf 600 Pixel skaliert und gleichzeitig umbenannt werden. Mit einem Blick in die Manpage bzw. mit dem Befehl "convert -help" können wir uns beliebige Optionen heraussuchen und in das Script einsetzen. Folgendes Beispiel macht automatisch diese Funktionen:

Bilder skalieren
Qualität auf 50% setzen
Eigene Schriftart verwenden
Schriftgröße festlegen
Positionierung festlegen
Text für Schriftzug festlegen

Beispiel-Script

for i in *.jpg;
do convert -scale 600 -font /fonts/terminat.ttf
-pointsize 12 -fill white -draw "text 350,445 'Copyright by PC-Erfahrung.de'"
$i pc_anleitung_$i urlaub_ten06_`basename $i .jpg`.jpg;
done

Was haben wir gemacht? Wir haben lediglich die benötigten Optionen hinter das convert geschrieben. Das Ergebnis ist folgendes:

Übersicht der Optionen

Usage: convert [options ...] file [ [options ...] file ...] [options ...] file

Where options include:
-adjoin join images into a single multi-image file
-affine matrix affine transform matrix
-annotate geometry text
annotate the image with text
-antialias remove pixel-aliasing
-append append an image sequence
-authenticate value decrypt image with this password
-average average an image sequence
-background color background color
-bias value add bias when convolving an image
-black-threshold value
forces all pixels below the threshold into black
-blue-primary point chromaticity blue primary point
-blur geometry blur the image
-border geometry surround image with a border of color
-bordercolor color border color
-channel type apply option to select image channels
-charcoal radius simulate a charcoal drawing
-chop geometry remove pixels from the image interior
-clip clip along the first path from the 8BIM profile
-clip-path id clip along a named path from the 8BIM profile
-clone index clone an image
-coalesce merge a sequence of images
-colorize value colorize the image with the fill color
-colors value preferred number of colors in the image
-colorspace type alternate image colorspace
-combine combine a sequence of images
-comment string annotate image with comment
-compose operator set image composite operator
-composite composite image
-compress type type of pixel compression when writing the image
-contrast enhance or reduce the image contrast
-convolve coefficients
apply a convolution kernel to the image
-crop geometry cut out a rectangular region of the image
-cycle amount cycle the image colormap
-debug events display copious debugging information
-define format:option
define one or more image format options
-deconstruct break down an image sequence into constituent parts
-delay value display the next image after pausing
-delete index delete the image from the image sequence
-density geometry horizontal and vertical density of the image
-depth value image depth
-despeckle reduce the speckles within an image
-display server get image or font from this X server
-dispose method GIF disposal method
-dither apply Floyd/Steinberg error diffusion to image
-draw string annotate the image with a graphic primitive
-edge radius apply a filter to detect edges in the image
-emboss radius emboss an image
-encoding type text encoding type
-endian type endianness (MSB or LSB) of the image
-enhance apply a digital filter to enhance a noisy image
-equalize perform histogram equalization to an image
-evaluate operator value
evaluate an arithmetic, relational, or logical expression
-extent geometry set the image size
-extract geometry extract area from image
-family name render text with this font family
-fill color color to use when filling a graphic primitive
-filter type use this filter when resizing an image
-flatten flatten a sequence of images
-flip flip image in the vertical direction
-floodfill geometry color
floodfill the image with color
-flop flop image in the horizontal direction
-font name render text with this font
-format "string" output formatted image characteristics
-frame geometry surround image with an ornamental border
-fuzz distance colors within this distance are considered equal
-fx expression apply mathematical expression to an image channel(s)
-gamma value level of gamma correction
-gaussian geometry gaussian blur an image
-geometry geometry perferred size or location of the image
-green-primary point chromaticity green primary point
-gravity type horizontal and vertical text placement
-help print program options
-identify identify the format and characteristics of the image
-implode amount implode image pixels about the center
-insert index insert last image into the image sequence
-intent type type of rendering intent when managing the image color
-interlace type type of image interlacing scheme
-label name assign a label to an image
-lat geometry local adaptive thresholding
-level value adjust the level of image contrast
-limit type value pixel cache resource limit
-log format format of debugging information
-loop iterations add Netscape loop extension to your GIF animation
-map filename transform image colors to match this set of colors
-mask filename set the image clip mask
-matte store matte channel if the image has one
-mattecolor color frame color
-median radius apply a median filter to the image
-modulate value vary the brightness, saturation, and hue
-monitor monitor progress
-monochrome transform image to black and white
-morph value morph an image sequence
-mosaic create a mosaic from an image sequence
-motion-blur geometry
simulate motion blur
-negate replace every pixel with its complementary color
-noise radius add or reduce noise in an image
-normalize transform image to span the full range of colors
-opaque color change this color to the fill color
-ordered-dither NxN
ordered dither the image
-orient type image orientation
-page geometry size and location of an image canvas (setting)
-paint radius simulate an oil painting
-ping efficiently determine image attributes
-pointsize value font point size
-posterize levels reduce the image to a limited number of color levels
-preview type image preview type
-profile filename add, delete, or apply an image profile
-quality value JPEG/MIFF/PNG compression level
-quiet suppress all error or warning messages
-radial-blur angle radial blur the image
-raise value lighten/darken image edges to create a 3-D effect
-random-threshold low,high
random threshold the image
-region geometry apply options to a portion of the image
-raise value lighten/darken image edges to create a 3-D effect
-red-primary point chromaticity red primary point
-render render vector graphics
-repage geometry size and location of an image canvas
-resample geometry change the resolution of an image
-resize geometry resize the image
-roll geometry roll an image vertically or horizontally
-rotate degrees apply Paeth rotation to the image
-sample geometry scale image with pixel sampling
-sampling-factor geometry
horizontal and vertical sampling factor
-scale geometry scale the image
-scene value image scene number
-seed value pseudo-random number generator seed value
-segment values segment an image
-separate separate an image channel into a grayscale image
-sepia-tone threshold
simulate a sepia-toned photo
-set attribute value set an image attribute
-shade degrees shade the image using a distant light source
-shadow geometry simulate an image shadow
-sharpen geometry sharpen the image
-shave geometry shave pixels from the image edges
-shear geometry slide one edge of the image along the X or Y axis
-sigmodial-contrast geometry
lightness rescaling using sigmoidal contrast enhancement
-size geometry width and height of image
-solarize threshold negate all pixels above the threshold level
-splice geometry splice the background color into the image
-spread amount displace image pixels by a random amount
-strip strip image of all profiles and comments
-stroke color graphic primitive stroke color
-strokewidth value graphic primitive stroke width
-stretch type render text with this font stretch
-style type render text with this font style
-support factor resize support: > 1.0 is blurry, < 1.0 is sharp
-swap indexes swap two images in the image sequence
-swirl degrees swirl image pixels about the center
-texture filename name of texture to tile onto the image background
-threshold value threshold the image
-thumbnail geometry create a thumbnail of the image
-tile filename tile image when filling a graphic primitive
-tint value tint the image with the fill color
-transform affine transform image
-transparent color make this color transparent within the image
-treedepth value color tree depth
-trim trim image edges
-type type image type
-undercolor color annotation bounding box color
-units type the units of image resolution
-unsharp geometry sharpen the image
-verbose print detailed information about the image
-version print version information
-view FlashPix viewing transforms
-virtual-pixel method
virtual pixel access method
-wave geometry alter an image along a sine wave
-weight type render text with this font weight
-white-point point chromaticity white point
-white-threshold value
forces all pixels above the threshold into white
-write filename write images to this file

</body> </html>