newbie question: 'store' info about orientation?

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
penguin

newbie question: 'store' info about orientation?

Post by penguin »

Hi all,

I just discovered ImageMagick and already it made a few things a lot easier, I really can't understand how I missed this for so long and spend countless hours with stupid manual workarounds...

What I'm doing right now is resizing a bunch of images in a folder. Images are different sizes and end up being 900x1200, my image magick line looks like this:

Code: Select all

convert %%a -rotate -90">" -resize 900x1200^^ -gravity center -extent 900x1200  c:\resized\%%a
This works perfectly and just as i want it to.

Some of the images are landscape images though, I rotate these because the whole bunch goes into some photoshop actions afterwards and for that its very handy to have all the images in the same size and orientation.

Now the question is - obviously it would be nice to have some way of knowing afterwards which of the images originally were landscape orientation to automatically rotate them back accordingly. I thought about a number of possible solutions but can't seem to really get it. How would you do this, what's the best way to go here? (Main goal being of course to spare me any manual sorting outside of my batchfile... :))

Thanks for your help,
cheers,
jo

PS I'm using IM on Win XP in case that makes any difference...
PPS I don't know if IM touches Exif-data at all, just in case, my images do not necessarily have any EXIF anyway...
penguin

Re: newbie question: 'store' info about orientation?

Post by penguin »

Meanwhile I found out about the 'info:' to get information about the image, this looks like a good starting point I guess...

I looked at some of the options to find out if there's any that just returns the orientation of the image - that would be perfect because I could check that and rename the file if IM returns 'landscape' for example. Is there such an option?

All I found is options that return the dimensions of the image, like so:

c:\image1.jpg JPEG 1024x768 8-bit DirectClass 128KB 0.109u 0:00.125

- I guess it might be possible to get the actual orientation from that line in windows batch, don't really know how, though... Any tips? Or, preferrably, an IM option thats better suited for this task?

Thanks in advance,
Jo
penguin

Re: newbie question: 'store' info about orientation?

Post by penguin »

Update:

identify -format %w gives me the width, identify -fomat %h gives me the height of an image, so now all i have to do is compare these two to get the orientation of the image...

unfortunately, I'm about to go nuts about the windows batch syntax. In order to compare the two IM outputs I'd have to have them in variables, so I tried _everything_ along the lines of:

Code: Select all

For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do set dh=%%J
...because according to google thats the way to store the output of a command such as IM into a variable... The bloody batch language however refuses to store anything into %dh%... if I do this:

Code: Select all

For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do echo %%J
it echoes the correct heigth of every Image as it should... now how the f**k do I store these values into a variable for comparison???

Aaaargh! Please please please you people out there, give me any comment, if you don't know what I'm talking about please please please let me know, dammit, the MS scripting syntax really is a nightmare...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: newbie question: 'store' info about orientation?

Post by fmw42 »

Don't know if this is relevant to your issue or not but see the recent post at viewtopic.php?f=1&t=16784
penguin

Re: newbie question: 'store' info about orientation?

Post by penguin »

Thanks fmw42, unfortunately (or fortunately?) I seem to be past that stage.... As of yet the following seems to be the case:

- iterating over a folder full of JPG in MS batch works fine
- rotating, resizing, cropping with IM all work fine
- getting info about a pictures orientation with IM identify works fine

- storing this info into a MS-batch-variable fails miserably.

So what I really need at this point is a good forum on MS batch programming. Haven't found one yet.

What I try to do now is something like this:

Code: Select all

@echo off
c:
cd PictureIn

FOR %%a in (*.jpg) do (

	For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do set dh=%%J
	For /F "Tokens=*" %%I in ('identify -format "%%w" %%a') Do set dw=%%I

	if %dw% GTR %dh%
		rename %%a "landscape_"%%a

	convert %%a -rotate -90">" -resize 800x1200^^ -gravity center -extent 800x1200 c:\PictureOut\%%a


)

@echo on
cd c:\
I got the 'for /F tokens=...' stuff from the web, and basically it seems to work OK to get the image dimensions from an image via IM, see two posts up. However, I can't get the script to store those values into variables.

Any help would be greatly appreciated, or if somebody could point me to a good forum on MS batch programming...?

Thanks,
Jo
penguin

Re: newbie question: 'store' info about orientation?

Post by penguin »

PS: here's two batch scripts -

this one works perfectly and prints out the correct image dimensions of all images in the folder:

Code: Select all

@echo off
c:
cd PictureIn

FOR %%a in (*.jpg) do (

	For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do echo %%J
	For /F "Tokens=*" %%I in ('identify -format "%%w" %%a') Do echo %%I
)

@echo on
cd c:\
This one should basically do the same as the above, but store the IM results in a variable inbetween. It doesn' work - seems to be a problem with the variable assignment as it always and only prints out the dimensions of the last image in the folder:

Code: Select all

@echo off
c:
cd PictureIn

FOR %%a in (*.jpg) do (

	For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do set dw=%%J
	For /F "Tokens=*" %%I in ('identify -format "%%w" %%a') Do set dh=%%I
	echo %dw%x%dh%
)

@echo on
cd c:\
Like I said any help / ideas / suggestions would be appreciated, I tried about _everything_, resetting the variables first thing in the loop, putting double/single/non % all over the place, putting " instead of ' and vice versa... with funny results, but none that worked. :(

Thanks,
Jo
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: newbie question: 'store' info about orientation?

Post by fmw42 »

Sorry, I am on a Mac/unix and only script in bash. I know nothing about Windows/Batch scripts. But there are plenty of others who can likely help you.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: newbie question: 'store' info about orientation?

Post by Drarakel »

Personally, I would use ExifTool and store the information in the orientation tag - for example like that:

Code: Select all

@echo off
SETLOCAL
set in=C:\PictureIn
set out=C:\PictureOut
set files=*.jpg
xcopy %in%\%files% %out%\temp\ /q
exiftool -Orientation#=6 -if "$ImageWidth > $ImageHeight" -overwrite_original %out%\temp\%files%
mogrify -path %out% -format tiff -rotate "-90>" -resize "800x1200^" -gravity center -extent 800x1200 -compress zip %out%\temp\%files%
rd /s /q %out%\temp
Then the landscape-images could be 'auto-rotated' again at a later date.

For your renaming-script, you will need some adjustments:
(Of course, further tweaks are possible. But the script should work with that.)

Code: Select all

@echo off
c:
cd PictureIn

SETLOCAL EnableDelayedExpansion
FOR %%a in (*.jpg) do (

	For /F "Tokens=*" %%J in ('identify -format "%%h" "%%a"') Do set dh=%%J
	For /F "Tokens=*" %%I in ('identify -format "%%w" "%%a"') Do set dw=%%I

	if !dw! GTR !dh! (
		rename "%%a" "landscape_%%a"
	)
)
FOR %%a in (*.jpg) do (

	convert "%%a" -rotate -90">" -resize 800x1200^^ -gravity center -extent 800x1200 "c:\PictureOut\%%a"


)

@echo on
cd c:\
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: newbie question: 'store' info about orientation?

Post by el_supremo »

[edit] never mind :-)

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Post Reply