Skip to content

Instantly share code, notes, and snippets.

@mcauser
Last active December 29, 2015 15:39
Show Gist options
  • Select an option

  • Save mcauser/7691971 to your computer and use it in GitHub Desktop.

Select an option

Save mcauser/7691971 to your computer and use it in GitHub Desktop.
Imagemagick.md

Imagemagick snippets

Thumbnails

Fit within 150x150

-strip removes colour profiles and must be before -thumbnail.

-thumbnail is an optimised version of -resize and removes image profiles.

eg. 600x300 -> 150x75

mkdir thumb
find . -maxdepth 1 -type f -exec convert {} -strip -thumbnail 150x150 -set filename:f '%t' 'thumb/%[filename:f].jpg' \;

# using -exec
find . -iname "*.jpg" -maxdepth 1 -exec convert -strip -thumbnail 150x150 {} thumb/{} \;

# using xargs
find . -iname "*.jpg" -maxdepth 1 | xargs -i{} convert -strip -thumbnail 150x150 {} thumb/{}

Large

Fit within 1000x1000

-quality for jpeg/miff/png compression.

1 = highest compression, 100 = best quality.

The default is to use the estimated quality of your input image if it can be determined, otherwise 92.

-resize increases or decreases the image size.

\> means only shrink larger and \ is an escape char.

eg. 2000x1500 -> 1000x750

mkdir large
find . -maxdepth 1 -type f -exec convert {} -strip -resize 1000x1000\> -quality 80 -set filename:f '%t' 'large/%[filename:f].jpg' \;

Fill

Resize to fill 442x297 then crop any overflow

-gravity specifies where to crop from. Default is NorthWest.

^ means fill given area

-extent sets the desired dimensions.

mkdir fill
find . -maxdepth 1 -type f -exec convert {} -resize 442x297^ -gravity Center -extent 442x297 -set filename:f '%t' 'fill/%[filename:f].jpg' \;

First page of pdf

The [n] after the filename means grab the nth frame.

convert test.pdf[0] test.jpg
mkdir thumb
convert test.pdf[0] -strip -thumbnail 150x150 -set filename:f '%t' 'thumb/%[filename:f].jpg'

Converted twice with density for high detail thumbnails and no unexpected black backgrounds.

$0 is the source filename when calling -exec sh -c

${0%.pdf} means remove .pdf from $0

find *.pdf -maxdepth 1 -type f -exec sh -c 'echo "Processing $0"; convert -density 300 "$0"[0] jpg: | convert - -strip -resize 150x150\> thumb/${0%.pdf}.jpg' {} \;

Identify

identify original/*.jpg
identify -verbose original/*.jpg

Bad examples

Uses way too much memory

convert original/*.jpg -strip -resize 800x800\> -quality 80 -set filename:f '%t' 'large/%[filename:f].jpg'
convert original/*.jpg -strip -thumbnail 100x100 -set filename:f '%t' 'thumb/%[filename:f].jpg'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment