DEV Community

Cover image for Fun with ImageMagick

Posted on

Fun with ImageMagick

ImageMagick is an amazing software suite, with tools for displaying images, editing, converting from one format to another and it works with both bitmap (raster) images and vector images. It can read over 100 major file formats, including RAW files from various camera models, video files such as AVI or MPEG and more.

Being command-line mostly tool, and ubiquitous, makes it the ideal tool to bundle with other software that needs image editing. It can be scripted - and some people have created some amazing script collections like Fred and Snibgo.

This post is a compilation of some of my random notes and various examples from ImageMagick usage pages. I provide no explanation for the various tools and options, the official site (especially the usage pages) and the man-pages explain it much better than I can.

ImageMagick command-line tools.

Here is the full list of all ImageMagick command-line tools. Some common tools are:

  • convert: image editing.
  • mogrify: image editing, mostly used for batch editing.
  • import: screenshot utility.
  • display: ImageMagick GUI.
  • animate: animates an image on a GUI window.

1. Create a text logo:

Check available fonts:

convert -list font

and then:

convert -background DarkSlateBlue -strokewidth 2 -stroke turquoise1 -font Aka-AcidGR-LivingSword -pointsize 256 -density 90 label:ListNUX test.png

text logo with imagemagick

2. Resize images:

Let's download some penguin images from Wikipedia:

mkdir -p penguins && cd "$_"
wget -i url_list.txt

First, let's resize all photos so that the biggest size (height or width, whichever is largest) is at maximum 800px. We will also reduce quality to 90%, for smaller file sizes.

for i in *.jpg; do
  convert $i -resize "800x800>" -strip -quality 90 _${i};
  rm ${i};

3. Crop images from center:

And let's crop them all to 500x500px squares:

for i in *.jpg; do
  convert ${i} -gravity center -crop 500x500+0+0 +repage _${i};
  rm ${i};

Now, let's rename them all to pengu01, pengu02, etc.

for i in *.jpg; do
  new=$(printf "pengu%02d.jpg" "$num");
  mv -- "$i" "$new"
  let num=num+1

5. Combine multiple images into a gif:

And now, time to animate!

# pop-up a window with all the images with 90sec delay
animate -delay 90 *.jpg
# write the previous animation to a gif image
convert -delay 90 *.jpg test.gif

gif image from previously downloaded static pictures

6. Create a gradient:

# simple linear gradient, two colours
convert -size 600x400 gradient:blue-red linear_gradient.png
# simple radiant gradient, two colours
convert -size 600x400 radial-gradient:blue-red radial_gradient.png
# a more complex, multi-colour gradient with bicubic interpolation
convert \( -size 600x400 gradient: -interpolate Bicubic -rotate 180 \
  \( +size xc:firebrick4 xc:crimson xc:MediumPurple xc:DarkSlateBlue -append \) \
  -clut \) \

Gradient created with ImageMagick

7. Create a text logo with a 3D effect:

convert gradient.png -background purple -alpha shape \
  -font Aka-AcidGR-ScrachThis -pointsize 192 -fill firebrick2 \
  -stroke black -gravity Center \
  -annotate 0 "ListNUX" logo_simple.png

Overlay the text:

convert logo_simple.png -alpha extract -blur 0x6 -shade 580x80 -normalize \
  logo_simple.png -compose Overlay -composite \
  logo_simple.png -alpha on -compose Dst_In -composite \

... and then add some shadow

convert logo_v1.png \
  \( +clone -background CornflowerBlue -shadow 80x4+20+20 \) \
  +swap -background MidnightBlue -layers merge +repage \

text logo with 3D effects

8. Stars:

Let's create an image with stars. We will create background noise, then delete and blur the excess noise, and use motion blur to create starbrusts.

convert -size 500x500 xc: +noise Random -channel R -threshold 0.07% \
  -negate -channel RGB -separate plasma:DarkOrchid1 \
  +channel \( +clone \) -compose multiply -flatten \
  -virtual-pixel tile  -blur 0x.5 \
  \( -clone 0  -motion-blur 0x20+15 -motion-blur 0x20+195 \) \
  \( -clone 0  -motion-blur 0x20-45  -motion-blur 0x20+135 \) \
  -compose screen -background black -flatten \
  -normalize stars.png

star field effect


Top comments (0)