Annotating photos with metadata -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?".
schievelbein
Posts: 12
Joined: 2013-10-24T11:34:48-07:00
Authentication code: 6789

Annotating photos with metadata -SOLVED

Post by schievelbein »

I am a genealogist and have thousands of photos that I want to annotate and update at any given time.
I want to maintain the integrity of the original photos, so I am splicing an annotation area to the bottom, so if I need to update the annotation, I can simply trim off the old and add another. The annotations are coming from the metadata that I am maintaining using gThumb in Linux, not that it should matter. The problem is that some of the photos have a LOT of people and the descriptions can be very long. I do not want the font to get too small, so I need word wrap. I thought that CAPTION with its word wrap might be able to help, but I cannot see to get it to work. The second issue that I might have is whether the spliced area will be tall enough. If I could know how many lines I was going to have, I could change my splice to be a multiple of the lines. My first Convert works, but the second does not. Thank you for any suggestions.

Code: Select all

read cwidth csplice cfont cdate ctime cdesc <<< $( identify -format "%w %[fx:h*.05] %[fx:h*.04] %[EXIF:DateTimeOriginal] %[IPTC:2:120]" $1 )
convert $1 -gravity South -background Black -pointsize $cfont -fill White -splice 0x${csplice} -annotate +0+0 "$cdesc" annotated_$1
#convert $1 -gravity South -background Black -pointsize $cfont -fill White -splice 0x${csplice} -size $cwidth caption:"$cdesc" annotated_$1
Last edited by schievelbein on 2017-04-05T08:41:49-07:00, edited 2 times in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating phots

Post by Bonzo »

Out of interest you may be able to use less commands with Imagemagick version 7 - what version are you using?
schievelbein
Posts: 12
Joined: 2013-10-24T11:34:48-07:00
Authentication code: 6789

Re: Annotating photos with metadata

Post by schievelbein »

I am currently using 6.7.7 on Linux Ubuntu, I will look into updating.
What suggestions do you have if I switch to Ver 7?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating photos with metadata

Post by Bonzo »

You should be able to use the fx calculations and EXIF data directly in the convert line.

For instance:

Code: Select all

magick "input.jpg" -gravity South -background Black -pointsize 10 -fill White -splice 0x%[fx:h*.04] -annotate +0+0 %[EXIF:DateTimeOriginal] "annotated.jpg"
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating photos with metadata

Post by Bonzo »

I was thinking about something along the lines of this but it will not work with caption as I get a geometry error:
magick: geometry does not contain image `input.jpg' @ warning/attribute.c/GetImageBoundingBox/240.
Here is the code with an -annotate for testing:

Code: Select all

magick "input.jpg" ( -clone 0 -fill Black  -colorize 100 -gravity north -pointsize 20 -fill White -annotate +0+0 "This is a very very long line and I will repeat some more blurb to make it a bit longer and longer" -trim ) -append "annotated.jpg"
It reads in the original image, copies it and changes it to black. Adds the white text to the now black copy; trims it down to the text size and joins the two images. This process would probably need to add an -extent or similar to get the width back to the size of the original.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Annotating photos with metadata

Post by fmw42 »

Did you try adding +repage after the -trim?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating photos with metadata

Post by Bonzo »

This also sort of works but the text wraps on a very narrow column !

Code: Select all

magick "input.jpg" ( -clone 0 -size %[fx:w]x%[fx:h] -delete 0 -background black -gravity center -pointsize 20 -fill White caption:"This is a very very long line and I will repeat some more blurb to make it a bit longer and longer" -trim ) -append "annotated.jpg"
Code modified as per fmw42's post below.
Did you try adding +repage after the -trim?
No I didn't fmw42 but just tried and get the same geometry error
Last edited by Bonzo on 2017-04-04T13:43:35-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Annotating photos with metadata

Post by fmw42 »

[fx:w]x[fx:h]
Change to

Code: Select all

%[fx:w]x%[fx:h]
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating photos with metadata

Post by Bonzo »

Thank you fmw42 that seems to have fixed it.

It looks to me like the caption needs a size defined to work.
schievelbein
Posts: 12
Joined: 2013-10-24T11:34:48-07:00
Authentication code: 6789

Re: Annotating photos with metadata

Post by schievelbein »

Thank you for the suggestions.
I have changed my code to read as follows:

Code: Select all

convert "input.jpg" ( -clone 0 -size %[fx:w]x%[fx:h] -delete 0 -background black -gravity center -pointsize 20 -fill White caption:"This is a very very long line and I will repeat some more blurb to make it a bit longer and longer" -trim ) -append "annotated.jpg"
I am getting a syntax error and cannot figure it out.
Any recommendation would be greatly appreciated.
Last edited by schievelbein on 2017-04-04T13:43:03-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating photos with metadata

Post by Bonzo »

My example above worked with caption and text but on testing fails with the %[EXIF:DateTimeOriginal] you may have to go back to reading that data into a variable first.

Did you try my example code with one of your images as it seems to do what you want except for the EXIF and IPTC data?
schievelbein
Posts: 12
Joined: 2013-10-24T11:34:48-07:00
Authentication code: 6789

Re: Annotating photos with metadata

Post by schievelbein »

I tried with a couple of my images and I receive the following:
./annotate: line 8: syntax error near unexpected token `('
./annotate: line 8: `convert "input.jpg" ( -clone 0 -size %[fx:w]x%[fx:h] -delete 0 -background black -gravity center -pointsize 20 -fill White caption:"This is a very very long line and I will repeat some more blurb to make it a bit longer and longer" -trim ) -append "annotated.jpg"'

Thank you very much for your help.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Annotating photos with metadata

Post by Bonzo »

On windows you need ( ) and I think on Linux ( and mac? ) you need to use \( \) try that.
schievelbein
Posts: 12
Joined: 2013-10-24T11:34:48-07:00
Authentication code: 6789

Re: Annotating photos with metadata

Post by schievelbein »

Thank you very much for the suggestions. The CAPTION is working as advertised, however, now, I am having trouble with the parameters.
I have tried many variation and cannot get the $cdesc and $cw or any of the other parameters to work. Any suggestion on being able to pass these parameters to Size and Caption would be appreciated.

Code: Select all

read cw ch cs cf cd ct cdesc <<< $( identify -format "%w %h %[fx:h*.05] %[fx:h*.04] %[EXIF:DateTimeOriginal] %[IPTC:2:120]" $1 )

convert "input.jpg" \( -clone 0 -size 1200 -delete 0 -background black -gravity center -pointsize 20 -fill White caption:"This is a very very long line and I will repeat some more blurb to make it a bit longer and longer This is a very very long line and I will repeat some more blurb to make it a bit longer and longer This is a very very long line and I will repeat some more blurb to make it a bit longer and longer" -trim \) -append "annotated.jpg"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Annotating photos with metadata

Post by fmw42 »

Perhaps your $1 is not getting resolve. Try using an actual image as a test.

What does your first command have to do with the subsequent convert command. I see no variables being passed.
Post Reply