Page 1 of 1

Identify only retrieves partial IPTC info - BUG ??

Posted: 2010-03-21T18:26:11-07:00
by paddy
Hi guys,

** Moved this from another thread: viewtopic.php?f=1&t=15778&p=56606#p56606

For some reason I always seem to have problems getting the "identify" command to parse correctly within a DOS batch file. :roll:

The latest problem I am having is that when using identify (in a batch file) to retrieve IPTC info from an image, the identify only seems to bring back characters up to the first blank character.

For example, if the image contains the following text in the [2:05] tag: "Tents for Paddies"
...then the following command executed at the command prompt:
identify -format "%[IPTC:2:05]" test.jpg
correctly returns: "Tents for Paddies"
However, when incorporated into a batch file like so:
for /f %%i in ('identify -format "p_caption=%%[IPTC:2:05]" test.jpg') do SET %%i
REM verification
for %%d in (p_caption) do ( echo %%d is !%%d! )
it only returns: "Tents"

This happens for any tags containing blanks ... eg. [2:25] Keywords; [2:80] By-Line

For reference info on the tags see: http://www.imagemagick.org/script/escape.php .. the section under %[IPTC:dataset:record]


Any ideas???

Re: Identify only retrieves partial IPTC info - BUG ??

Posted: 2010-03-21T18:31:56-07:00
by el_supremo
Looks like you may have missed this reply in the original thread:
kankwayot wrote:The problem you encounter with blanks does not come from identify, it's that some are by default delimiters for for ..
-> try to replace for /f %%i
by for /f "delims=" %%i
which is such that no delimiter is defined.
The for statement has a myriad of options and variations on its syntax. See http://www.computerhope.com/forhlp.htm
Pete

Re: Identify only retrieves partial IPTC info - BUG ??

Posted: 2010-03-22T09:26:50-07:00
by jaffamuffin
for /f %%i in ('identify -format "p_caption=%%[IPTC:2:05]" test.jpg') do SET %%i
Be careful with quotes in quotes - you may want to rewrite that as (with the delims trick as well)

Code: Select all

for /f "usebackq delims=" %%i in (`identify -format "p_caption=%%[IPTC:2:05]" test.jpg`) do SET %%i
As per the info on that linked page.
usebackq specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset.
Also is that code a fragment? as just typing SET %%i won't do much - you need to SET p_caption=%%i ?

Then echo %p_caption%

Re: Identify only retrieves partial IPTC info - BUG ??

Posted: 2010-03-23T17:38:23-07:00
by kankwayot
jaffamuffin wrote: Also is that code a fragment? as just typing SET %%i won't do much - you need to SET p_caption=%%i ?

Then echo %p_caption%
[i][url=http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=15778#p56606]In the other thread[/url],[/i] paddy wrote: Thanks! ... That is a useful (if syntactically challenging :D) optimization.
It appears that this way to use a DOS script variable, as necessary as it was, can lead to confusion.

To see things with more clarity, it's good to have in mind that for a DOS script all variables are strings.

Which explains for example why one must explicitly use the flag /A with set to perform arithmetic operations, as in set /A "two=1+1" to get the string 2 instead of the string 1+1.

The other thing to grok is that writing a line that has variables is similar in DOS scripting to write a formatted string that has named parts. When the script is run, the named parts are expanded and the resulting string, which is by now hopefully valid code, is then interpreted :

Code: Select all

set "code=echo test"
%code%
.. does display test.

In the case at hand, the variable %%i already contains p_caption= ; once expanded, SET %%i gives SET p_caption=whatever_value_is_identified.
Afterward, displaying p_caption by use of a for loop can look like overkill but quickly becomes convenient when other variables must be inspected too.

Re: Identify only retrieves partial IPTC info - BUG ??

Posted: 2010-03-24T05:01:04-07:00
by paddy
jaffamuffin wrote:
for /f %%i in ('identify -format "p_caption=%%[IPTC:2:05]" test.jpg') do SET %%i
Be careful with quotes in quotes - you may want to rewrite that as (with the delims trick as well)

Code: Select all

for /f "usebackq delims=" %%i in (`identify -format "p_caption=%%[IPTC:2:05]" test.jpg`) do SET %%i
As per the info on that linked page.

usebackq specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset.
Thanks for that. Unfortunately I was using single quotes rather than backquotes. :oops:
(Note to self.... "reduce screen resolution so I can distinguish different types of quotes!" :D )

You'll notice that I also had to do a bit of extra work to get it to work. The IPTC string had to be quoted in a very specific way, which meant I had to insert some extra quotes, then strip them out again later to make it work, but this is more to do with the way other parts of my script is written.

jaffamuffin wrote:Also is that code a fragment? as just typing SET %%i won't do much - you need to SET p_caption=%%i ?

Then echo %p_caption%
FYI. here's the code in context (which might make it clearer what's going on with that strange syntax:

Code: Select all

	set p_caption=
	for /f "usebackq delims=" %%x in (`identify -format """%%[IPTC:2:05]""" %infolder%\%imgname%%imgsuffix%`) do set p_IPTC=%%x
	for /f "usebackq delims=" %%i in (`identify -format "width=%%[fx:w]\nheight=%%[fx:h]" %infolder%\%imgname%%imgsuffix%`) do SET %%i
	REM verification
	for %%d in (p_IPTC width height) do ( echo %%d is !%%d! )

	echo. p_IPTC (%imgname%) : %p_IPTC%
	echo. imgname: %imgname%

	if [%p_IPTC%]==[""] ( ^
		set p_caption=%imgname%
	) ELSE (
		set p_caption=%p_IPTC:"=%
	)
.......plus the explanation by kankwayot above explains it quite well. ( The joys of DOS! :shock: )

Cheers

Re: Identify only retrieves partial IPTC info - BUG ??

Posted: 2010-05-06T03:33:09-07:00
by leomartin6
Thanks for the help. I'll definitely keep that in my mind while doing my work.