Page 1 of 1

How to get Image's width and height in STDOUT

Posted: 2018-09-01T15:25:49-07:00
by AnotherGeorge
The short version: how to get an image's width and height ONLY in STDOUT. The long version:

I'm an Imagick novice using Windows 7 Pro. I've been using Imagick for about four years, but only to mine image data for JPSoft (https://jpsoft.com) scripts for image selection and housekeeping. I find the Imagick capabilities a bit intimidating and far beyond anything I feel like tackling at this time. I may look into using it for image resizing+sharpening, but not until I get a much simpler problem resolved. I want a direct way to get an image's width and height directly with a command that uses more than just "identify". ALL I need is the (UNIX) command-line structure to get it done, and I can probably convert that to work in my scripts.

I have a test script that does this:
magick identify "%imgTest" > IMIdent.txt

Where the output file consists of this:
[Image PATH]\800x600 JPG.jpg JPEG 800x600 800x600+0+0 8-bit sRGB 265265B 0.000u 0:00.000

Then I get the 7th field from the right (8th if it's a GIF) and manipulate that for the width, height and ARatio. After discovering Imagick, I toyed with more sophisticated commands copied/pasted from examples, so I think I have the savvy to handle the width+height command.

I know this is a woefully trivial matter, but It is really annoying me. I was going to make another script that will prepend the "ARatio Width-x-Height" to the front of images, when it occurred to me that Imagick would surely have a direct way to get at least the width and height. I spent the better part of two hours scouring the Internet and got some good news and some bad news.

Good News: There were dozens of websites that showed exactly how to do it.
Badd News: None of them worked, they only scolded me for not knowing what I was doing. Also, Forum searches didn't come close to addressing something so simple a subject.

Most were earlier than Imagick v7 and probably all of them were for UNIX (with which I am familiar) but they all had the command-line structure. I tried things like
identify -format %[EXIF:*]
identify - format "%[fx:w]x%[fx:h]"
identify -format "%w x %h" info:

The most promising for v7 was using "identify -size geometry", but all I got was another scolding. I have the technical savvy after IT experience using UNIX and 30+ years writing scripts that integrate third-party tools. I just need to know what the REAL command is.

Thanks

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-01T16:18:27-07:00
by snibgo
For v7, a command line for width and height is:

Code: Select all

magick identify -format "%wx%h" toes.png
Use your own image instead of toes.png, of course. "%w" is a metadata escape. For others, see http://www.imagemagick.org/script/escape.php

The format can contain other text:

Code: Select all

magick identify -format "width is %w\nheight is %h" toes.png

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-02T20:13:57-07:00
by AnotherGeorge
Thank You Snibgo for your reply. Unfortunately, the commands didn't work - but I got them to work. I studied your first command in depth and my comments are below. I tried your second command only in a JPSoft script and got the same change-in-results as in the first command detailed below.

REGARDING THE FIRST COMMAND:

The STDOUT was just "h" (witout the quotation marks). BUT - It did give me the proper command structure that I had confidence would work.

First, I tried it in a JPSoft script with my aliases and variables. The STDOUT was just the "h". Then I tried it again, but with double %% which tells the script to consider them to be a single literal percentSign and not a variable designator - like this: "%%wx%%h". It worked; the STDOUT was "800x600".

Then I created a BAT file with the same single-% and double-%% commands. The "h" and "800x600" results were the same. The entire contents of the BAT file and resulting STDOUT are below.

I do appreciate your taking the time to reply. I hope I am able to contribute something to the Imagick community with this verbose thread update.

AnotherGeorge

The contents of the BAT file are between the ======= lines:
===========================================================
@echo off
echo.
echo About to execute original command from Forum:
pause
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" identify -format "%wx%h" "E:\ERoot\0800x0600.jpg"

echo.
echo.
echo About to execute the command with double percentSigns:
pause
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" identify -format "%%wx%%h" "E:\ERoot\0800x0600.jpg"

echo.
echo.
echo About to exit
pause
exit

===========================================================


The resulting STDOUT (copied from the "shell" window) is between the ***** lines:
***********************************************************

About to execute original command from Forum:
Press any key to continue . . .
h

About to execute the command with double percentSigns:
Press any key to continue . . .
800x600

About to exit
Press any key to continue . . .
***********************************************************

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-02T20:50:07-07:00
by AnotherGeorge
Me Again.

It occured to me that I had executed the BAT from my "Total Commander" file manager instead from a Windows7 "shell". That would be a proper, more universal form of execution. Here are the results from doing that (copied from the window):

****************************************************************
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Windows\System32>E:

E:\>cd ERoot

E:\ERoot>dir
Volume in drive E is Elvira
Volume Serial Number is C6C4-C1B3

Directory of E:\ERoot

09/02/2018 08:37 PM <DIR> .
09/02/2018 08:37 PM <DIR> ..
08/04/2018 06:36 AM 265,265 0800x0600.jpg
09/02/2018 07:45 PM 421 TestIMg.bat
2 File(s) 265,686 bytes
2 Dir(s) 750,505,664,512 bytes free

E:\ERoot>testimg

About to execute original command from Forum:
Press any key to continue . . .
h

About to execute the command with double percentSigns:
Press any key to continue . . .
800x600

About to exit
Press any key to continue . . .
*************************************************************

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-03T03:09:28-07:00
by snibgo
The Windows BAT shell needs % to be doubled. With the interactive command shell (CMD), don't double them.

More Windows IM info at http://www.imagemagick.org/Usage/windows/ , and my page has many examples (almost all using BAT).

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-05T07:43:14-07:00
by AnotherGeorge
It would seem my Windows7 Pro is broken. I repeated the BAT execution from a "cmd.exe" window and got the same results as before.

I made a video of the process I followed - the same as before except started by executing cmd.exe. I made a little website with the video, the BAT file and the JPG file (the JPG is not really necessary, but just to be consistent). There is a button to download a ZIP file with the BAT and JPG, or you can scroll down and copy the BAT text and SaveAs the JPG.

The website is:

rshenterprises.org/anothergeorge

I recommend you just copy/paste that to get the spelling and text case right. I tested the website with Chrome, Firefox, Pale Moon and Opera browsers. I don't have MS Internet Explorer on my PC because MS tried to impose its installation and integration into my OS a few years ago. The video might be a bit jerky at the start as it is being downloaded, but you can just play it again and it will be smooth. The video is a little big ("little" "big"?) for the website, but I wanted to make it as clear as possible.

Let me know if I can do more to reconcile what I'm getting versus what others, more versed in the ImageMagick usage, know how it is supposed to behave from a BAT file.

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-05T08:47:43-07:00
by snibgo
Sorry, I'm lost. Do you have a problem? If so, what is it?

Your command with the double percents works perfectly. The format is "%%wx%%h" so the output is 800x600.

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-06T13:32:34-07:00
by AnotherGeorge
No Problem. You said on Sept-03 "With the interactive command shell (CMD), don't double them". I am not familiar with an "interactive command shell (CMD)". I stated in my first thread input that I was using Windows7 Pro. I only know about CMD as the cmd.exe shell (pseudo shell in Win7) which I used in the video. I presumed your (CMD) comment was germane to my Win7 issue. I guess I presumed wrong. Which raises the question: Why didn't you enter the double-percentSign command in your first thread input?

But, I had fun making the video and that website; so, making them wasn't a complete waste of time spent trying to address what I thought was a worthy matter to pursue for my benefit and that of any others else who might have had their curiosity peaked.

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-06T13:35:58-07:00
by AnotherGeorge
Notice to Forum Administrator: The subject of this thread has been concluded and this thread can be closed.

AnotherGeorge

Re: How to get Image's width and height in STDOUT

Posted: 2018-09-06T15:44:21-07:00
by snibgo
"CMD" is the interactive command window. You type a command line (or paste it in), press "enter" and the command is executed immediately. % signs shouldn't normally be doubled.

"BAT" is where you edit a file aka script, eg myfile.bat, and type a command or paste it into the file. After you have saved the file, you instruct the shell to run the script, ie to execute the commands in it. BAT files need the double % signs.