Identify only retrieves partial IPTC info - BUG ??

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
paddy

Identify only retrieves partial IPTC info - BUG ??

Post 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???
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
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.
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

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

Post 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%
kankwayot

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

Post 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.
paddy

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

Post 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
leomartin6

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

Post by leomartin6 »

Thanks for the help. I'll definitely keep that in my mind while doing my work.
Post Reply