Page 1 of 1

Conditional convert/mogrify based on dimensions?

Posted: 2009-01-03T15:53:41-07:00
by nmerriam
The short form of the question:
Is there a straightforward way to only mogrify/convert images whose dimensions are larger than 1080x1920?

The details:
I have directories and subdirectories with many JPEGs of varying dimensions. I have a great mogrify command that adjusts and resizes those images down to 1080x1920, and I'm happy with the results.

Code: Select all

find * -iname '*.jpg' -print0 | xargs -0 mogrify -contrast-stretch .3%,99.7% -resize 1080x1920\> -monitor -quality 80


There are two problems with this:
1) regardless of the dimensions of the image, mogrify will still apply the contrast-stretch
2) regardless of whether the image needs resizing, mogrify will rewite (and thus recompress) the file, losing quality

What I'd like is for mogrify to completely ignore any image under 1080x1920 and not process it at all. I can get the dimensions of the image by using identify, but there doesn't seem to be any way to make use of that information short of grep. Is there any way to have identify or another tool pipe a list of only images above 1080x1920? I'm on OS X and AppleScript through Image Events actually has an easy way to do this, where a script can read an image's dimensions and then test if they are greater than or less than some amount, but I'd like something more portable.

Re: Conditional convert/mogrify based on dimensions?

Posted: 2009-01-03T17:25:49-07:00
by fmw42
see -resize with the > option http://www.imagemagick.org/script/comma ... php#resize

you may need to use quotes or escape

-resize 100x100>

-resize '100x100>'

-resize 100x100\>

or on windows

-resize 100x100^>

Re: Conditional convert/mogrify based on dimensions?

Posted: 2009-01-03T17:41:45-07:00
by nmerriam
Thanks, FMW, I'm already using the ">" option, which does prevent it from enlarging images that are smaller than my desired size. The problem remains that it still rewrites (and recompresses, losing quality) the images, even if they are already the correct size. I'm hoping someone knows a way to disable all processing based on resolution.

Re: Conditional convert/mogrify based on dimensions?

Posted: 2009-01-03T18:02:59-07:00
by fmw42
nmerriam wrote:Thanks, FMW, I'm already using the ">" option, which does prevent it from enlarging images that are smaller than my desired size. The problem remains that it still rewrites (and recompresses, losing quality) the images, even if they are already the correct size. I'm hoping someone knows a way to disable all processing based on resolution.

Best way that I know is to use a script conditional and test the size.

Re: Conditional convert/mogrify based on dimensions?

Posted: 2009-01-03T18:27:17-07:00
by nmerriam
fmw42 wrote:Best way that I know is to use a script conditional and test the size.
Awesome, that's exactly what I'm looking for -- what scripting technique could I use to test the condition of image dimensions? I don't see a built-in way with "identify" or "find" or other common Bash utilities. Do you know of a way to accomplish this test and pass the results on to a pipe or a file?

Re: Conditional convert/mogrify based on dimensions?

Posted: 2009-01-03T20:14:34-07:00
by fmw42
nmerriam wrote:
fmw42 wrote:Best way that I know is to use a script conditional and test the size.
Awesome, that's exactly what I'm looking for -- what scripting technique could I use to test the condition of image dimensions? I don't see a built-in way with "identify" or "find" or other common Bash utilities. Do you know of a way to accomplish this test and pass the results on to a pipe or a file?

This is one example of how you can do it. If the image (in this case the IM internal image rose:) is smaller than 30x30 in either dimension then it will not be processed.

infile="rose:"
outfile="rose100.jpg"
width=`identify -ping -format %w $infile`
height=`identify -ping -format %h $infile`
echo "width=$width; height=$height"
[ $width > 30 -a $height > 30 ] && convert $infile -resize 100x100 $outfile


see string formats http://www.imagemagick.org/script/escape.php