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!
Newbie learning ImageMagick - conditional resize
-
- Posts: 3
- Joined: 2016-12-19T19:57:19-07:00
- Authentication code: 1151
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Newbie learning ImageMagick - conditional resize
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Newbie learning ImageMagick - conditional resize
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.
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.
snibgo's IM pages: im.snibgo.com
-
- Posts: 3
- Joined: 2016-12-19T19:57:19-07:00
- Authentication code: 1151
Re: Newbie learning ImageMagick - conditional resize
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Newbie learning ImageMagick - conditional resize
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.
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.
snibgo's IM pages: im.snibgo.com
-
- Posts: 3
- Joined: 2016-12-19T19:57:19-07:00
- Authentication code: 1151
Re: Newbie learning ImageMagick - conditional resize
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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Newbie learning ImageMagick - conditional resize
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.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.
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"
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.