Page 1 of 1
Newbie learning ImageMagick - conditional resize
Posted: 2016-12-19T20:03:20-07:00
by justinb001
Hello everyone, new to the board. I'm using ImageMagick 7.0.4-0 Q16 x64 on a Windows machine. Running it from the Windows command line. I've read the helpful guides and references posted on ImageMagick but still struggling with this seemingly simple command.
Here's what I'm trying to do: I have a large folder of JPEGs that are 3600x2400 in size. Some of the images are rotated (so they are 2400x3600).
I am trying to use the -shave command in mogrify to trim off a certain amount of pixels from the height and from width.
Since I need to use different settings for the portrait-oriented images vs. the landscape-oriented, I'm trying to find out how to write the command so that it executes ONLY if the image is the width and height I specify.
Any help you can provide is appreciated. Thank you!
Re: Newbie learning ImageMagick - conditional resize
Posted: 2016-12-19T20:45:33-07:00
by fmw42
I do not think you can do that in mogrify, since it likely does not respect inline % escapes and %[fx:...] computations. You will have to write a script for the conditional to control the subsequent magick command to do the shaving.
Re: Newbie learning ImageMagick - conditional resize
Posted: 2016-12-19T20:48:57-07:00
by snibgo
I don't think this can be done in a single "mogrify". So you need a loop, doing a "magick" for each file.
With v7, I expect you can use complex "fx:" expressions within the "-shave", so it shaves the amount you want according to whether the image is portrait or landscape.
So, you need to work out the command you need for portrait images, and the command you need for landscape images. While doingthat, figure out using "fx:" within "-shave". Then combine the two commands into one.
Re: Newbie learning ImageMagick - conditional resize
Posted: 2016-12-19T20:52:03-07:00
by justinb001
Thanks for all the great help. Seeing as I'm on Windows, is there a good starter pack to writing and running scripts for Magick? Do I need to install special software like Cygwin?
Re: Newbie learning ImageMagick - conditional resize
Posted: 2016-12-19T21:02:10-07:00
by snibgo
Windows BAT scripts can be used. If you know Windows, that is easiest.
If you know bash, then you can install Cygwin or Microsoft bash for Windows 10.
There are many examples of BAT scripts in my pages.
Re: Newbie learning ImageMagick - conditional resize
Posted: 2016-12-19T21:13:55-07:00
by justinb001
Great. i'll pick through those and try to come up with something and post back with any follow-up questions. thanks for the help
Re: Newbie learning ImageMagick - conditional resize
Posted: 2016-12-20T01:16:23-07:00
by GeeMack
justinb001 wrote:Here's what I'm trying to do: I have a large folder of JPEGs that are 3600x2400 in size. Some of the images are rotated (so they are 2400x3600). I am trying to use the -shave command in mogrify to trim off a certain amount of pixels from the height and from width.
Since I need to use different settings for the portrait-oriented images vs. the landscape-oriented, I'm trying to find out how to write the command so that it executes ONLY if the image is the width and height I specify.
ImageMagick is so flexible you can run some powerful IM commands from some very simple BAT scripts. What you describe above could be done right from the command line if your input files only come in those two sizes and if you don't have to check for errors.
Using ImageMagick 7 on Windows 10 64, I can open a command window, navigate to a directory full of JPG files, and paste in a command like this at the prompt...
Code: Select all
md done & for %I in ( *.jpg ) do magick "%I" -set filename:f "done/%~nI" ^
-shave "%[fx:w==2400?25:15]x%[fx:w==2400?45:35]" -quality 100 "%[filename:f].jpg"
That would first create a directory named "done" to take the output files from the command. Then it would run a "for" loop over all the JPG files.
The IM command inside the loop would shave 25 pixels from the left and right sides if the image is 2400x3600, or 15 pixels off those sides if it's 3600x2400. It would also shave 45 pixels from the top and bottom if it's a 2400x3600 image, and 35 pixels if the input image is 3600x2400.
The command would create each output image with the same name as the input images, and put them all in the "done" directory.
You could put that same command into Windows BAT script by making every single percent sign "%" into a double "%%".
Using FX expressions within ImageMagick commands is slightly ambitious, but it's quite powerful for bulk processing, especially for some of these conditional situations. You can learn more about FX expressions
at THIS link.