Conditional convert/mogrify based on dimensions?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
nmerriam

Conditional convert/mogrify based on dimensions?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional convert/mogrify based on dimensions?

Post 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^>
nmerriam

Re: Conditional convert/mogrify based on dimensions?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional convert/mogrify based on dimensions?

Post 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.
nmerriam

Re: Conditional convert/mogrify based on dimensions?

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional convert/mogrify based on dimensions?

Post 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
Post Reply