How to use extent conditionally, based on image size?

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
TheBladeRoden
Posts: 2
Joined: 2017-09-25T07:55:25-07:00
Authentication code: 1151

How to use extent conditionally, based on image size?

Post by TheBladeRoden » 2017-09-25T08:07:42-07:00

I figured out how to enlarge only pictures that are smaller than 1440x900, but the same technique doesn't work for extent (i.e. it will clip a 1600x1080 picture down to 1440x900 instead of leaving it alone)

Code: Select all

mogrify -path results -resize 1440x900^< -extent 1440x900^< -background black -gravity center -quality 95 *.jpg
PAUSE
I'm using version 7.0.7 with a Windows bat file.

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use extent conditionally, based on image size?

Post by snibgo » 2017-09-25T08:39:35-07:00

In v7, you can put fx: expressions in the extent, such as something like this (untested):

Code: Select all

-extent "%[fx:w<1440?1440:w]x%[fx:h<900?900:h]"
For Window BAT, double the % signs.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to use extent conditionally, based on image size?

Post by GeeMack » 2017-09-25T08:41:45-07:00

TheBladeRoden wrote:
2017-09-25T08:07:42-07:00
I figured out how to enlarge only pictures that are smaller than 1440x900, but the same technique doesn't work for extent (i.e. it will clip a 1600x1080 picture down to 1440x900 instead of leaving it alone).
My testing seems to indicate that "mogrify" isn't compatible with FX expressions, so I'd probably run the process as a "for" loop using "magick" instead of "mogrify". Something like this in a BAT script should do pretty much what you're trying to accomplish...

Code: Select all

for %%I in ( *.jpg ) do (
   magick "%%I" -set filename: "%%[t]" -resize 1440x900^< -gravity center ^
   -background black -extent "%%[fx:max(w,1440)]x%%[fx:max(h,900)]" "result\%%[filename:].jpg"
)

User avatar
fmw42
Posts: 22109
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: How to use extent conditionally, based on image size?

Post by fmw42 » 2017-09-25T10:09:20-07:00

Only magick (replacement for convert) allows %[fx: ...] computation inline for arguments. Mogrify, montage, etc do not.

TheBladeRoden
Posts: 2
Joined: 2017-09-25T07:55:25-07:00
Authentication code: 1151

Re: How to use extent conditionally, based on image size?

Post by TheBladeRoden » 2017-09-25T14:35:39-07:00

GeeMack wrote:
2017-09-25T08:41:45-07:00
TheBladeRoden wrote:
2017-09-25T08:07:42-07:00
I figured out how to enlarge only pictures that are smaller than 1440x900, but the same technique doesn't work for extent (i.e. it will clip a 1600x1080 picture down to 1440x900 instead of leaving it alone).
My testing seems to indicate that "mogrify" isn't compatible with FX expressions, so I'd probably run the process as a "for" loop using "magick" instead of "mogrify". Something like this in a BAT script should do pretty much what you're trying to accomplish...

Code: Select all

for %%I in ( *.jpg ) do (
   magick "%%I" -set filename: "%%[t]" -resize 1440x900^< -gravity center ^
   -background black -extent "%%[fx:max(w,1440)]x%%[fx:max(h,900)]" "result\%%[filename:].jpg"
)
Hey that worked out pretty well, thanks!

Post Reply