Page 1 of 1
Command line magick, using image size in command?
Posted: 2017-04-03T13:11:53-07:00
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.
Re: Command line magick, using image size in command?
Posted: 2017-04-03T13:54:41-07:00
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:".
Re: Command line magick, using image size in command?
Posted: 2017-04-03T14:24:39-07:00
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 "%%".
Re: Command line magick, using image size in command?
Posted: 2017-04-03T14:35:02-07:00
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
Re: Command line magick, using image size in command?
Posted: 2017-04-03T17:01:47-07:00
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.
Re: Command line magick, using image size in command?
Posted: 2017-04-03T17:20:56-07:00
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
Re: Command line magick, using image size in command?
Posted: 2017-04-04T02:27:20-07:00
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.