Skip to content

Instantly share code, notes, and snippets.

@wesort
Last active February 19, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesort/0170483140b8a7347823af672161dea5 to your computer and use it in GitHub Desktop.
Save wesort/0170483140b8a7347823af672161dea5 to your computer and use it in GitHub Desktop.
How to batch process images with bash & Imagemagick

Resize files and append height to filename with bash & Imagemagick

  • Run each command separately
  • Move files to correct directory mv * ~/path/to/image_directory

Resize keeping aspect ratio (useful for srcset)

NB: create each directory first

mkdir 350px 600px 900px 1400px 1800px

convert "*.jpg" -monitor -resize 1800x -quality 80 -set filename:area "1800px/%t_h%wpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 1400x -quality 80 -set filename:area "1400px/%t_h%wpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 900x -quality 80 -set filename:area "900px/%t_h%wpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 600x -quality 80 -set filename:area "600px/%t_h%wpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 350x -quality 80 -set filename:area "350px/%t_h%wpx" %[filename:area].jpg


Thumbnails: resize & crop to standard aspect ratio

NB: create thumbnails directory first mkdir thumbnails

w:  200  |  400  |  600  |  900
h:  260  |  520  |  780  |  1170

convert "*.jpg" -monitor -resize 200x260^ -crop 200x260+0+0 -quality 80 -set filename:area "thumbnails/%t_thumb-w%w-h%hpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 400x520^ -crop 400x520+0+0 -quality 80 -set filename:area "thumbnails/%t_thumb-w%w-h%hpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 600x780^ -crop 600x780+0+0 -quality 80 -set filename:area "thumbnails/%t_thumb-w%w-h%hpx" %[filename:area].jpg

convert "*.jpg" -monitor -resize 900x1170^ -crop 900x1170+0+0 -quality 80 -set filename:area "thumbnails/%t_thumb-w%w-h%hpx" %[filename:area].jpg


An individual file

sizes: 350px 600px 900px 1400px 1800px

convert "filename-01.jpg" -monitor -resize 1800x -quality 80 -set filename:area "%t_h%wpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 1400x -quality 80 -set filename:area "%t_h%wpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 900x -quality 80 -set filename:area "%t_h%wpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 600x -quality 80 -set filename:area "%t_h%wpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 350x -quality 80 -set filename:area "%t_h%wpx" %[filename:area].jpg


Individual file thumbnails: resize & crop to standard aspect ratio

w:  200  |  400  |  600  |  900
h:  260  |  520  |  780  |  1170

convert "filename-01.jpg" -monitor -resize 200x260^ -crop 200x260+0+0 -quality 80 -set filename:area "%t_thumb-w%w-h%hpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 400x520^ -crop 400x520+0+0 -quality 80 -set filename:area "%t_thumb-w%w-h%hpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 600x780^ -crop 600x780+0+0 -quality 80 -set filename:area "%t_thumb-w%w-h%hpx" %[filename:area].jpg

convert "filename-01.jpg" -monitor -resize 900x1170^ -crop 900x1170+0+0 -quality 80 -set filename:area "%t_thumb-w%w-h%hpx" %[filename:area].jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment