ImageMagick Basics for Batch Processing Comics


Need to resize your comics for a new site?
You can save a lot of time by resizing and renaming the files in bulk, rather than doing it all by hand. The best way I know how to do this is with ImageMagick (it's free). This site is a guide to help you get started with IM.


Installing ImageMagick

On Mac
  • Download and install MacPorts
  • Open Terminal (it's in Applications, under Utilities)
    • If you haven't used Terminal before, here are some basics:
      • You can see what folder you're currently in by typing pwd
      • You can move from folder to folder by typing cd folder-name-here
      • You can go back one folder by typing cd ..
  • In Terminal, type sudo port install ImageMagick to install ImageMagick
  • Once install has completed, type mogrify -help and see if anything happens. If you don't get a 'function not found' error, the install has worked!
On Windows
  • Download and install Cygwin. When you get to the Package Install page during set-up, search for ImageMagick and download those packages. (This may involve changing "View" to "Full" and clicking the icon next to whatever's under "New" so that a checkbox appears next to Src?. Basically, do whatever you have to to check that checkbox.)

  • Open Cygwin, under Programs
  • If you haven't used a terminal like Cygwin before, here are some basics:
    • You can see what folder you're currently in by typing pwd
    • You can move from folder to folder by typing cd folder-name-here
    • You can go back one folder by typing cd ..
    • To access folders in your main C:/ drive, you want to type /cygdrive/c/your/path/here; e.g. cd /cygdrive/c/Users/myUsername/Downloads could be the thing you need to access your Downloads folder
  • In Cygwin, type mogrify -help and see if anything happens. If you don't get a 'function not found' error, the install has worked!

Basics

Once you're over the hurdle of getting ImageMagick installed, you can process large numbers of images with just a few lines of code. There are multiple ways of handling the basics in ImageMagick, but I'm just going to describe one way for each in order to keep things simple.
Converting filetype
  • Use cd to navigate to the folder containing the images you want to convert
  • Type mkdir output to create a folder called 'output' where the new images will be saved
  • To change the file format, we're going to use the -format flag. Typing the line mogrify -path output -format png *jpg into your command window (Terminal or Cygwin) and pressing enter will take all the JPEG files in your current folder, convert them to png, and save the outputs into the folder 'output'
  • Similarly, running the line mogrify -path output -format tif *png will convert all the PNG files in your current folder to TIFF and save the outputs in the folder 'output'
Resizing
  • Use cd to navigate to the folder containing the images you want to convert
  • Type mkdir output to create a folder called 'output' where the new images will be saved
  • To change the image size, we're going to use the -resize flag. With the resize flag, you specify a size and the image is resized to fit (not fill) that size. Typing the line mogrify -resize 940x8000 -path output *png into your command window and pressing enter will take all the PNG files in your current folder, resize them to the largest size that fits inside a rectangle of width 940px and height 8000px, and save the outputs to the 'output' folder. For all but very, very tall images, this will be the same as changing the width to 940 px (and if you comic is very tall, just replace 8000px with a higher number until it works).
  • Similarly, running the line mogrify -resize 1200x8000 -path output *jpg will convert all the JPG files in your current folder to fit inside a rectangle of size 1200x8000 and save the result to the 'output' directory

You can combine these functions if you want to resize and change format on a folder of images. For instance, the line mogrify -path output -format tif -resize 1200x8000 *png changes all the PNGs in your current folder to TIFFs, resizes them to fit inside the rectangle of width 1200 px and height 8000 px, and saves them to the 'output' folder.

A Little Fancier

Thumbnails
You can generate random thumbnails for a large folder of images by automatically cropping the images to a square of the right size.
  • Navigate to the folder containing your images using cd
  • Create a folder for thumbnails by typing mkdir thumbnails and pressing enter
  • You can choose what part of the images you want to sample for the thumbnails by using the -gravity flag. For instance, mogrify -gravity northwest -crop 300x300+0+0 -path thumbnails/ *tif takes all the TIFF files in your current folder, cuts out a 300 px by 300 px square from the northwest corner, and saves the output to a folder called 'thumbnails'
  • Similarly, mogrify -gravity center -crop 300x300+0+0 -path thumbnails/ *png takes a 300 pixel square from the center of all the PNG files in your folder and saves it to the 'thumbnails' folder

One image, multiple sizes
Sometimes you only have one image, and you want to resize it to multiple widths. You can do that by copying and pasting code like the following into the command window: for i in 500 940 1000 1200 1500; do mogrify -resize "$i"x10000 -quality 100 -write output"$i".png input-image-name.png; done In the above, 500 940 1000 1200 1500 are the widths I want to resize my image to (The 10000 pixels part is a filler height I put in to make sure that the resized image matches the desired width; it's just there to be big). My input image name is 'input-image-name.png' and the output of running this code are five PNG files, named output500.png, output940.png, output1000.png, output1200.png, and output1500.png.

Renaming for dates
Sometimes you want to rename a bunch of files so that their names correspond to dates. I don't have code that will work for all use cases written up, but I'm happy to help people get code that will work for their needs. Below is sample code that can be run in the Mac terminal. It takes a folder of images, called 'input', as input, resizes them to width 1200 pixels, and renames them so that each filename has the format 'comicYYMMDD.tif', with one comic per week, starting from tomorrow.

count=0;
initialDay=1;

for filename in input/*; do
echo "$filename";

shift=$((initialDay + count*7));
i=`date -v +"$shift"d "+%y%m%d"`;
echo "$i";

count=$((count+1));
mogrify -resize 1200x20000 -format tif -write comic"$i".tif "$filename";
done

So this code could take a folder of 200 images and rename them 'comic170328.tif', 'comic170404.tif', 'comic170411.tif', etc. (This code probably won't work in Cygwin, because the date function operates differently). If you're interested in running code like this, let me know and we can try to figure it out.