How to conditionally resize canvas. [SOLVED]
Posted: 2015-01-30T02:47:03-07:00
Hey ImageMagick/Windows/cmd prompt pros. I'm using IM 6.9.0-3 on Windows 7 x64. I have a directory of many images that need to be "corrected" to a 4:3 aspect ratio. They are all "auto-cropped", and are of various sizes and aspect ratios due to the nature of the image content. I don't want to resize or crop the images because that will either result in quality reduction or cutting out of desired image content. Want I want to do is add white space to either the sides or top/bottom to achieve the 4:3 aspect ratio, but do so based on the current aspect ratio. Then I want to "pad" with an extra 10px of white space on all sides. This condition means that I must find the width and height separately and apply a "-extent" to either the width or height. I'm not well versed in batch scripting or IM (complete n00b). I've made a first attempt at the script to give you a start and help you better understand what I'm trying to do. Any suggestions to get this code working would be wonderfully appreciated. Again, I'm a complete noob, so feel free to just scrap all my code and write something completely different. Thanks!
Code: Select all
@echo off
cd c:\images
FOR %%i IN (*.jpg) DO (
FOR /f %%w IN ('identify -format "%w"') DO (
FOR /f %%h IN ('identify -format "%h"') DO (
IF %%w gtr %%h (
convert %%i -gravity center -extent %%w+10x%%w*3/4+10 "c:\images\canvasized\%%i"
) ELSE (
convert %%i -gravity center -extent %%h*4/3+10x%%h+10 "c:\images\canvasized\%%i"
)
)
)
)