Blueserv.net

Creating thumbnails using ImageMagick

What is the purpose of a thumbnail

I'm using thumbnails for the purpose of adding illustrative visuals that go together with the information links.

The definition according to Wikipedia:
"Thumbnails are reduced-size versions of pictures or videos, used to help in recognizing and organizing them, serving the same role for images as a normal text index does for words. In the age of digital images, visual search engines and image-organizing programs normally use thumbnails, as do most modern operating systems or desktop environments, such as Microsoft Windows, macOS, KDE (Linux) and GNOME (Linux)."

See https://en.wikipedia.org/wiki/Thumbnail.

How it's done, an example

A thumbnail can be easily created from an existing image file using the toolchain provided by ImageMagick.

Using convert you can define your input original and scale it within a new thumbnail file.

convert camera-playback.jpeg -thumbnail '240x128' -background white -gravity west -extent 128x128 camera-playback-thumb.jpeg

Parameters have the following purpose:

  • thumbnail - indicates that the image is converted in a thumbnail, hence scaled and this is the new size of the scaled image;
  • background - the color of the background of the new thumbnail. This is the visible part in case the thumbnail size is smaller then the size of the new image as inidicated by the parameter extent.
  • extent - the size of the new image;
  • gravity - the alignment of the original image as specified by the thumbnail size within the new image size. I.e. orientation as center, east, west as the new images positioned over the thumbnail.

Example of creating thumbnails from a photo, the historical Hong Kong Star Ferry in front of the famous Hong Kong skyline of Hong Kong Island:

Results into the following thumbnails:

Scaled linearly keeping the image aspect ratio Scaled linearly to thumbnail size scaled linearly to thumbnail size and positioned the image on the right scaled linearly to thumbnail width, cropping the image to fit scaled linearly to thumbnail width, cropping after positioning the image on the bottom side
convert star-ferry-original.jpeg -thumbnail 128x128 -background yellow -gravity center star-ferry-scaled.jpeg
convert star-ferry-original.jpeg -thumbnail 128x128 -background yellow -gravity center -extent 128x128 star-ferry-scaled2.jpeg
convert star-ferry-original.jpeg -thumbnail 128x128 -background yellow -gravity east -extent 128x128 star-ferry-scaled3.jpeg
convert star-ferry-original.jpeg -thumbnail 128x -background yellow -gravity center -extent 128x128 star-ferry-scaled4.jpeg
convert star-ferry-original.jpeg -thumbnail 128x -background yellow -gravity north -extent 128x128 star-ferry-scaled5.jpeg

Resizing & Overlay

convert star-ferry-original.jpeg -resize 480x320 star-ferry-original-small.jpeg
composite -gravity SouthEast overlay-small.png star-ferry-original-small.jpeg star-ferry-with-overlay.png