Imagemagick useful cmds: basic bash select, image resize, crop, creation

Standard

Imagemagick adapted personal notes.

Most times when I want to do something on several images I use artscript, a small GUI tool to automate most common, and not so common, imagemagick operations. It makes a breeze to prepare a set of images to display on one or several output sizes and formats. artscript backbone is imagemagick command line program, mainly what artscript does is compose the command line option to make imagemagick do its thing.

The remaining times artscript doesn’t fit my imagemagick needs I find it useful to remember the following commands.

General image selection

Use terminal utilities to filter the wanted images and feed the result to either *xargs* or a *for/do loop*. The most common utilities, and easier to remember are: ls and find.

ls

List all files in a directory, using wildcards from bash its possible to filter the results easily.

 $ ls *.png ## list all png
 $ ls draw_*.jpg ## list all jpg starting with 'draw'

find

For queries more complex or uncertain I use find. find queries can be very complex on its own

$ find . -name '*.ora' ## finds all file and dirs ending with '.ora' (used * wildcard again)
$ find . -type f -name '*.?[pn]?' -ctime -30 ## finds all png and jpg files in current dir created in the last 30 days.

## spaces in characters will break the pipe, use print0 to feed xargs
$ find . -name '*.jpg' -print0

When doing a for loop its not necessary to use xargs, and its possible to feed the bash mini script with patters, this is specially usefull when you know exactly the image you are going to process.

## Inspect all files patterns between 044, 045, 046.. until 050.
## any command can go after the do.
$ for f in 14_{030..050}.ora ; do file $f; done

## Using xargs
$ echo 14_{030..050}.ora | xargs file

Creating Images


Creating blank images, checkboards or colored rectangles can be useful. I use checkerboards for different patterns tests, single colored images for palette checking from the explorer using previsualizations, and other minor tasks.

convert -size 100x100 pattern:checkerboard PNG8:checker.png
	-auto-level # add to get black and whites

anycolor

$ convert -size 256x256 xc:"#333333" PNG8:scheme-gray.png
$ convert -size 100x100 xc:white -fill "#ff6b06" -opaque white PNG8:checker.png

gradient

$ convert -size 100x100 gradient:white-"#ff6b06" PNG8:checker.png

Making several colors in one command. Supposing you have all your colors (or sizes) written inside a text file, one for each line.

# each color in colors.txt is one for row like this "#ffffff", or "white"
# FILE will be replaced by the row content.
$ cat colors.txt | xargs -I FILE convert -size 256x256 xc:"FILE" PNG8:FILE.png

Reference:
http://www.imagemagick.org/Usage/canvas/#solid

Simple Resize and Crop

Simple cropping is needed when adding quick images taken from my desktop. I normally want to remove the desktop elements and focus on the area in question. Screen shots taken in succession are perfect for this as all elements remain in the same place.

# Cropping
# convert INPUT -crop widthxheight+xoffset+yoffset OUTPUT
$ for f in *.png; do convert $f -crop 1342x782+0+192 ${f%.png}.jpg; done

TIP: On mac and linux use “display” command to get the exact coordinates.

# On one image from the set run
$ display image_from_set.png

This will show the image in an X11 window. To get the crop coordinates:

  • Click image
  • Select Transform / Crop
  • Click and drag over the area you want to crop.
  • Coordinates will be shown at the top left corner (they could be obscured by the crop pop-up dialog)
  • Copy values, by hand and place them in your command.

Resizing

For simple and complex resizing artscript is my choice, it not only allows to output multiple sizes but also has many fitting options to avoid image distortion in cases in which a particular size is needed (This combines crop and resize in a single operation). I add the command for reference only but be sure to read the reference on resizing as shrinking and growing require different approaches to make the results great.

References:
This long thread in imagemagick forums:
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=17926

Up-sampling:
http://www.imagemagick.org/Usage/resize/#adaptive-resize
http://www.imagemagick.org/Usage/filter/nicolas/#upsampling

Down-sampling:
http://www.dpreview.com/forums/post/40282142

In general doing this by hand with imagemagick as you seen in the references requires some investigation and testing. For artscript I use samplings tailored for maximum quality in painting and illustration. Upscaling is the least common use for painters, designers and illustrators so I use a generally good filter and blur radius.

For shrinking artscript has tested many iterations, from calculating change percentage between the source size and destination to set a blur radius change, to apply a general value to all resizes, which is what is currently set, but with a catch. We add an unsharp mask based on size ratio changes.

The commands below are what artscript uses:

# For enlarging
# -interpolate bicubic -filter LanczosRadius -define filter:blur=.9891028367558475 -distort Resize ${finalscale}!

# For shrinking
# +sigmoidal-contrast 1.0 -filter Lanczos -define filter:blur=.9264075766146068 -distort Resize ${finalscale} -sigmoidal-contrast 1.0

# Example, replace $resize_cmd with the operation you need.
# $finalscale: 1280x650, 200x200.
$ convert input.png $resize_cmd -quality 90 output.jpg

By default imagemagick will fits the image inside the area you set for final resize respecting the original image aspect ratio. To force it to use the new size despite loosing aspect ratio you need to add some characters to the end of the final size string

  • ( ! ) As in 200×200! to force imagemagick to output the image in that size. Image will be distorted
  • ( ^ ) Fill given area. Image will cover the area given maintaining proportion. Some parts of the image will be cutout to do this.
  • ( > ) Only shrinks larger than
  • ( < ) Only enlarges smaller than

When working with extremely big image collages, or mosaics it is best to reduce imagemagick memory load by resizing on read. artscript uses this for collage mode, reducing time and memory usage on collage creation considerably.

# To load an image resized
$ convert image.png'[200x300]' image_out.jpg

Wrapping it all up

There are still a lot of notes missing in this useful commands notes, like compositing, making animated gifs (and controlling time output), effect output, preserve transparency. etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s