Page 1 of 1

batch resizing

Posted: 2012-02-08T17:48:39-07:00
by guitarcrazy44
I am using imagemagick under ubuntu. What algorithm would I use to resize all images in a directory to a resolution of 640x385, but ONLY if they have a resolution of 320x240.

Re: batch resizing

Posted: 2012-02-08T19:38:44-07:00
by fmw42
mogrify has the ability to resize all images in a folder, but I don't believe you can restrict it to a particular size.

You can probably do one of two things.

1) Write a bash shell script to check the image size and then resize the ones you want.

2) Write a script to find the images of the size you want and then feed those to mogrify to do the resize.


See
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics/#mogrify_not

You can extract the image size from any image using string formats:
http://www.imagemagick.org/script/escape.php

for example

size=`convert image -format "%wx%h" info:`

using the IM internal image logo:

size=`convert logo: -format "%wx%h" info:`
echo "$size"
640x480

Re: batch resizing

Posted: 2012-02-09T00:03:54-07:00
by anthony
I would use a pipeline...

Code: Select all

identify -format '%f %wx%h' images... | sed -n 's/ 320x240$//p' | xargs mogrify -resize 640x385\!
the identify lists the filename and size of all images
the sed looks for a specific size and removes the size leaving just the filename
the xargs command reads filenames, quotes them and added them to the end of the given command, "mogrify" in this case.

results is that only images with the matching sized are found and resized.

TEST thoroughly!

PS: using "xargs" is in the "mogrify_not" section of IM Examples, but using a '-I' option to make it run one command on each individual file "convert", rather than 'batch' multiple files in one command. Read its manpage.
A alternative is "parallel" from GNU which is a paralleling (run multiple command simultaneously) version of "xargs"