Fun with ImageMagick
Tags: imagemagick, commandline, fun
Published: 2019-07-17 23:20:06

[ImageMagick](https://imagemagick.org/) 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](https://imagemagick.org/script/formats.php#supported), 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](http://www.fmwconcepts.com/imagemagick/index.php) and [Snibgo](http://im.snibgo.com/).

This post is a compilation of some of my random notes and various examples from [ImageMagick usage pages](http://www.imagemagick.org/Usage/). I provide no explanation for the various tools and options, the official site (especially the [usage pages](http://www.imagemagick.org/Usage/)) 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](https://imagemagick.org/script/command-line-tools.php). 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:


```bash
convert -list font
```

and then:

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

+ [List of colour names](https://www.imagemagick.org/script/color.php#color_names).

Result:
![text logo with imagemagick](https://thepracticaldev.s3.amazonaws.com/i/rzeezj86n1753s5iff94.png)

## 2. Resize images:

Let's download some [penguin images from Wikipedia](https://en.wikipedia.org/wiki/Penguin):

```bash
mkdir -p penguins && cd "$_"
cat>"url_list.txt"<<EOF
https://upload.wikimedia.org/wikipedia/commons/0/08/South_Shetland-2016-Deception_Island%E2%80%93Chinstrap_penguin_%28Pygoscelis_antarctica%29_04.jpg
https://upload.wikimedia.org/wikipedia/commons/1/1d/Penguin_in_Antarctica_jumping_out_of_the_water.jpg
https://upload.wikimedia.org/wikipedia/commons/f/fd/Antarctic_adelie_penguins_%28js%29_21.jpg
https://upload.wikimedia.org/wikipedia/commons/4/49/Pygoscelis_antarcticus_-Cooper_Bay%2C_South_Georgia%2C_British_Overseas_Territories%2C_UK-8.jpg
https://upload.wikimedia.org/wikipedia/commons/9/9e/Penguins_walking_-Moltke_Harbour%2C_South_Georgia%2C_British_overseas_territory%2C_UK-8.jpg
https://upload.wikimedia.org/wikipedia/commons/4/4f/Yellow-eyed_Penguin_MC.jpg
EOF
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.

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

## 3. Crop images from center:

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

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


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

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

## 5. Combine multiple images into a gif:

And now, time to animate!

```bash
# 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](https://thepracticaldev.s3.amazonaws.com/i/cgesr1xcfxs8rnsq1bzy.gif)

## 6. Create a gradient:

```bash
# 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.png
```

![Gradient created with ImageMagick](https://thepracticaldev.s3.amazonaws.com/i/qo1r7nu5btov1jzivdqp.png)

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

```bash
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:

```bash
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 \
  logo_v1.png
```

... and then add some shadow

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

![text logo with 3D effects](https://thepracticaldev.s3.amazonaws.com/i/d2vjw6o0xitzrbnpx1hd.png)

## 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.

```bash
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](https://thepracticaldev.s3.amazonaws.com/i/f1zh5av0igada6698kp3.png)

---

*Credits*
+ <small>*ImageMagick logo [Wikimedia Commons](https://en.wikipedia.org/wiki/File:ImageMagick_logo.svg) under [ImageMagick License](http://www.imagemagick.org/script/license.php)*</small>
+ <small>*[Examples of ImageMagick usage](https://www.imagemagick.org/Usage/)*</small>
+ <small>*Fonts used: [Aka-acid](http://aka-acid.com/) fonts, specifically:*</small>
    - <small>*[Living sword](http://aka-acid.com/livingsword.html) font*</small>
    - <small>*[Scratch this](http://aka-acid.com/scratchthis.html) font*</small>