Overlay certain EXIF data onto image....

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
Posts: 2
Joined: 2011-04-21T13:19:05-07:00
Authentication code: 8675308

Overlay certain EXIF data onto image....

Post by AaronJAnderson »

Hello, very new to IM. Is it possible to read the exif data from an image and write it as an overlay?

Essentially, I'd want this printed on a corner of the image.

Code: Select all

exif:GPSLatitude: 38/1, 1535/100, 0/1
exif:GPSLatitudeRef: N
exif:GPSLongitude: 85/1, 4478/100, 0/1
exif:GPSLongitudeRef: W
exif:GPSTimeStamp: 21/1, 53/1, 5611/100
exif:GPSVersionID: 2, 2, 0, 0
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Overlay certain EXIF data onto image....

Post by Bonzo »

You do not say how you are going to use the code as you may be able to get the data using some other method and put it on the image.

Certain EXIF data can be read by Imagemagick but I am not sure if it will get the GPS data.

This is a batch file that will add the aperture and shutter speed to an image:

Code: Select all

:: Do not display the code while running

:: Select the F number from the EXIF data and set the FNumber variable
FOR /F %%x IN ('identify -ping -format "%%[EXIF:FNumber]" %1') DO SET FNumber=%%x

:: Set the FNumber1 variable to the F number value
:: Image Magick returns the F number in the format of 700/100 or 40/10
FOR /F %%x IN ('convert xc: -ping -format "%%[fx:%FNumber%]" info:') DO SET FNumber1=%%x

:: Set the value of the shutter variable to the shutter speed
:: Select the shutter speed from the EXIF data
:: Image Magick returns the shutter speed in the format of 810/100
FOR /F %%x IN ('identify -ping -format "%%[EXIF:ShutterSpeedValue]" %1') DO SET shutter=%%x

:: Format the speed to 8.1 and set the speed variable
FOR /F %%x IN ('convert xc: -ping -format "%%[fx:%shutter%]" info:') DO SET speed=%%x

:: Calculate the speed in seconds using the power of 2 and save it in the shutterspeed variable
FOR /F %%x IN ('convert xc: -ping -format "%%[fx:floor((pow(2,%speed%)))]" info:') ^
 DO SET shutterspeed=%%x

:: Add the F number and shutter speed to the image and save as exif_OriginalImageName
convert %INPUTFILE% ^
-pointsize 16 -fill black -gravity northwest ^
-annotate +10+5 "Aperture: F%FNumber1% Shutter speed: 1/%shutterspeed% sec" "%~p1EXIF_%~n1.jpg"  
User avatar
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlay certain EXIF data onto image....

Post by fmw42 »

I don't think IM knows about geo tags, but am not 100% certain. See the list of EXIF data that can be retrieved at http://www.imagemagick.org/script/escape.php. However, if the data is in the verbose information (identify -verbose info:) you may be able to write a script to extract that information. Also perhaps using EXIF tool you can extract the information you need and pass it to IM to write it in the image. see http://www.sno.phy.queensu.ca/~phil/exiftool/.

You also don't say what image format this is coming from, nor what version of IM you are using or platform.

Perhaps you can post a link to one of your images for others to test.
User avatar
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Overlay certain EXIF data onto image....

Post by anthony »

Getting the data is one step, and done two different ways.

One is using 'percent escapes'
The other is to extract the information in an external script using "identify" or some other tool (as previously mentioned)

Using 'percent escapes' has the advantage that you can immediately -annotate the information directly (as is) on the image

An external script method mean you can use the scripting language or other text processing tools to cut-up, and reformat the string exactly how you want it, then you can annotate that onto the image using a second image call.

The first is faster as it is all in one command, the second more versatile.

The information you specified can be extracted using a 'globbing' expression (case insensitive) in the percent escape.. For example... Any EXIF tag that has GPS in the tag name...

Code: Select all

identify -format "%[EXIF:*GPS*]" image.jpg
To annotate it directly (nothing fancy using default font)

Code: Select all

  convert image.jpg -gravity NorthWest -annotate 0 "%[EXIF:*GPS*]"  new_image.jpg

How you 'annotate' the information onto the image is the second matter, that you have not really addressed.

Do you want plain text, like the above, or something more fancy?
Do you want to append the information above or below (perhaps rotated on the side) on the image?
Do you want to add a 'dimmed box' or blurred background halo around the text to ensure it is always readable regardless of how light or dark the image is?
Do you want it to fit in a specific sized area, auto-size the font to fill that area, or word wrapped to the width of the image?
Etc Etc Etc...
There are lots and lots of possibilities you probably never even thought about.

This is all dealt in a whole section of IM examples, Annotating Images
Which is combined with the previous section, Compound Fonts,
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
Posts: 2
Joined: 2011-04-21T13:19:05-07:00
Authentication code: 8675308

Re: Overlay certain EXIF data onto image....

Post by AaronJAnderson »

Thanks Anthony, that's exactly what we needed. We're working on an interesting project. I'll send you the details if we deploy it!
Post Reply