How to conditionally resize canvas. [SOLVED]

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
mercyfall
Posts: 3
Joined: 2015-01-30T01:30:54-07:00
Authentication code: 6789

How to conditionally resize canvas. [SOLVED]

Post by mercyfall »

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"
			)
		)
	)
)
Last edited by mercyfall on 2015-02-04T02:29:50-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to conditionally resize canvas.

Post by snibgo »

"identify" needs to know what file you want information on. So, for example, one of your lines should be

Code: Select all

FOR /f %%w IN ('identify -format "%w" %%i') DO (
snibgo's IM pages: im.snibgo.com
mercyfall
Posts: 3
Joined: 2015-01-30T01:30:54-07:00
Authentication code: 6789

Re: How to conditionally resize canvas.

Post by mercyfall »

Thanks for the reply. You are correct, that was one thing I missed, passing the file name to the identify -format command. However, I think I still have many other problems with my code. It seems maybe I still don't fully understand how some of these commands work. I was using the FOR loop with the identify command as if the %%i being passed was the filename, but after reading through the IM windows usage wiki again I think the FOR loop examples they used where they passed a command in the IN statement was not traversing a set of files and passing those file names to the identify command, but were traversing a set of identify commands. For example,

Code: Select all

FOR /F %%L IN ('identify -format "Width=%%w\nHeight=%%h" %1') DO set %%L
loops through the identify -format command first with width, %1 pulls a fully qualified path name from somewhere (but from where?) and passes it to the identify width's value and set's it to %%L, then the FOR loop continues to height, which is then again set to %%L. Am I understanding that right now? If so, then my original code is all wrong.

I revised my code but now I don't know what's going wrong. I really dislike DOS prompt. Maybe if someone would be so kind as to write their own version of code that does what I'm trying to achieve. It seems like it would be so easy for someone who actually understands the syntax for DOS and IM. The logic is dead simple, it's just getting the syntax right that's kicking my @$$.

Code: Select all

@echo off

cd c:\images

FOR %%i IN ("*.jpg") DO (
	echo %%i
	set width=identify -format "%w" %%i
	echo %width%
	set height=identify -format "%h" %%i
	echo %height%
	IF %width% gtr %height% (
		echo "w is bigger")
	echo "h is greater"
)
I wrote that just to see if what I'm doing is right, but something is really wrong. The output tells me "-format was unexpected at this time." What the hell? If I only set width, leaving out anything related to height I get:

c:\images>canvasize.bat
RFM.JPG
identify -format "%w" TC2.JPG
indentify -format "i
SMT.JPG
identify -format "%w" TC2.JPG
indentify -format "i
TC2.JPG
identify -format "%w" TC2.JPG
indentify -format "i

This just doesn't make any sense to me. Maybe there is somebody that would be so kind as to just write their own version of code that does what I need? The logic is dead simple, but syntax is what's holding me back...
mercyfall
Posts: 3
Joined: 2015-01-30T01:30:54-07:00
Authentication code: 6789

Re: How to conditionally resize canvas.

Post by mercyfall »

I figured it out, thanks to the very last example on Bonzo's batch scripts page: http://rubblewebs.co.uk/imagemagick/batch.php

It got me off to a really good start. Within a few hours of customizing for my needs and debugging I was able to accomplish what I wanted. I'm multiplying everything by 100 because Windows cmd prompt can't do floating point arithmetic...

Code: Select all

@echo off
Setlocal enabledelayedexpansion

CD "<source directory>"

SET /A "tratio=100*4/3"

FOR /r %%i IN ("*.jpg") DO (

		FOR /f %%w IN ('identify -verbose -ping -format "%%[fx:w]" "%%i"') DO (SET /A "width=%%w")

		FOR /f %%h IN ('identify -verbose -ping -format "%%[fx:h]" "%%i"') DO (SET /A "height=%%h")   

		SET /A "ratio=100*!width!/!height!"	
		
		
		IF  !ratio! GTR !tratio! (
			SET /A "newHeight=3*!width!/4"
			convert "%%i" -background white -gravity center -extent !width!x!newheight! "<destination directory>\%%~nxi"
		) ELSE (
			SET /A "newWidth=4*!height!/3"
			convert "%%i" -background white -gravity center -extent !newWidth!x!height! "<destination directory>\%%~nxi"
		)

	echo canvasizing %%~nxi

)

PAUSE
Post Reply