Command line magick, using image size in command?

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
kosmarnik
Posts: 4
Joined: 2010-07-15T02:42:46-07:00
Authentication code: 8675308

Command line magick, using image size in command?

Post by kosmarnik »

Hi,
Currently I use 2 lines to run magick, one to get the image size, second to actually do the job.
Would it be possible to have it in one go?

Code: Select all

magick  image.jpg -profile sRGB.icc overlay.jpg -resize 1280x600 -gravity center -compose multiply -strip -define jpeg:extent=240000 -composite out.jpg
Why I do that is I want to resize image.jpg while keeping aspect ratio to a max 1280x1280, but if I enter -resize 1280x1280 than the overlay.jpg gets cropped, and I want the overlay.jpg to resized to fit the smallest side.

With two lines it's easy, but I have no clue how to go about it in one.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Command line magick, using image size in command?

Post by snibgo »

I'm not sure what you want to do, but perhaps "-set option:" and parentheses will help. For example (Windows CMD syntax):

Code: Select all

magick toes.png -resize 1280x1280 -set option:MYSIZE %[fx:min(w,
h)]x%[fx:min(w,h)] ( xc: -resize %[MYSIZE] ) info:

toes.png[0] PNG 1280x1117 1280x1117+0+0 16-bit sRGB 0.203u 0:00.031
xc:[1] XC 1117x1117 1117x1117+0+0 16-bit sRGB 0.188u 0:00.046
This takes toes.png, resizes it, and puts the lower dimension in MYSIZE, for example 1117x1117. Then it uses MYSIZE to resize "xc:".
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Command line magick, using image size in command?

Post by GeeMack »

kosmarnik wrote: 2017-04-03T13:11:53-07:00Why I do that is I want to resize image.jpg while keeping aspect ratio to a max 1280x1280, but if I enter -resize 1280x1280 than the overlay.jpg gets cropped, and I want the overlay.jpg to resized to fit the smallest side.
A command like this will resize your first image, maintaining its aspect, to make it fit inside a 1280x1280 space. Then it sets a variable by obtaining the width or height, whichever is smaller, from the resized input image. Then it resizes your overlay image to fit inside the space specified by that variable while also retaining its aspect...

Code: Select all

magick image.jpg -resize 1280x1280 -set option:newsize "%[fx:min(w,h)]x%[fx:min(w,h)]" ^
   ( overlay.jpg -resize "%[newsize]" ) -gravity center -compose multiply -composite out.jpg
Adjust your "-profile" and "-define" settings as needed. If you're working on a *nix shell or script, you'll have to change that continued line caret "^" to a backslash "\", and also escape those parentheses with backslashes "\( ... \)". If you're using it in a Windows BAT script you'll have to make all the single percent marks "%" into doubles "%%".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command line magick, using image size in command?

Post by fmw42 »

Just to be clear, since you did not say what version of IM you are using nor what platform, to do what snibgo and GeeMack suggest requires IM 7. If you are on IM 6, you cannot do this and would need two lines
kosmarnik
Posts: 4
Joined: 2010-07-15T02:42:46-07:00
Authentication code: 8675308

Re: Command line magick, using image size in command?

Post by kosmarnik »

Thank you people!
That was quick :)

I switched to IM 7 today, so no problem there.
Platform, I'm not even sure :)
Cygwin on Win 10. A mix of win and cygwin Perl.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command line magick, using image size in command?

Post by fmw42 »

On a Unix-like system, the syntax above needs to be changed to

Code: Select all

magick image.jpg -resize 1280x1280 -set option:newsize "%[fx:min(w,h)]x%[fx:min(w,h)]" \
   \( overlay.jpg -resize "%[newsize]" \) -gravity center -compose multiply -composite out.jpg
kosmarnik
Posts: 4
Joined: 2010-07-15T02:42:46-07:00
Authentication code: 8675308

Re: Command line magick, using image size in command?

Post by kosmarnik »

fmw42 wrote: 2017-04-03T17:20:56-07:00 On a Unix-like system, the syntax above needs to be changed to

Code: Select all

magick image.jpg -resize 1280x1280 -set option:newsize "%[fx:min(w,h)]x%[fx:min(w,h)]" \
   \( overlay.jpg -resize "%[newsize]" \) -gravity center -compose multiply -composite out.jpg
Indeed, it was late and I missed the quoted parentheses at first, they don't have to be escaped :)

Another weird thing is windows Perl and system calls, I could not for the life of me escape the % signs, but that's for another forum. Got around it by switching to cygwin Perl.
Post Reply