print metadata to jpg

Hi all,

I would like to write a scipt that gets gps informatoin from a jpg and print 's it on the lower left corner, In order to get the gps data I have found a tool called jhead. In know that with the help of the imagemagick command convert it is possible to print text on the pictures.

exemple of jhead output I would like to print.

alioune@baccus ~/disk-c/Users/alioune/Pictures $ jhead ML.JPG |grep "GPS"
GPS Latitude : N 14d 40.54m  0s
GPS Longitude: W 17d 27.82m  0s
GPS Altitude :  53.00m
alioune@baccus ~/disk-c/Users/alioune/Pictures $ 

Thx for your help.

Could you please give output of jhead , also what pattern you want to format from the output??:confused:

just make use this type code may helpful.:smiley:

 cat jpeg.sh
#!/bin/sh
for input in `ls -1 /your/jpg/file/path`
do
jhead $input | grep "GPS"
done
1 Like
source=ML.JPG
target=target.JPG

set -- $(identify $source |awk 'NR==1{split($3,a,"x"); print a[1],a[2]}')   # get the image's size, width=$1, length=$2, you need adjust for your image, depand the output format of command identify
                   
jhead $source |grep "GPS" |awk -v width=$1 -v length=$2 -v sfile=$source -v tfile=$target '
NR==1{Latitude=$0}NR==2{Longitude=$2} NR==3{Altitude=$0}
END{print "convert -pointsize 18 -font /path/to/font.ttf -fill white -stroke black -strokewidth 1 -draw \" "Latitude, " 10," length-72 "\" -draw \" "Longitude, " 10," length-54 "\" -draw \" "Altitude, "  10," length-36 "\" ",sfile, tfile}'

convert is imagemagick command. if output is fine, just copy the output and run it directly.

1 Like

Here is the Jhead output. but I only need th gps part.

alioune@baccus ~/GPStagtoPic $ jhead ML.jpg 
File name    : ML.jpg
File size    : 583257 bytes
File date    : 2010:12:28 16:45:05
Camera make  : Apple
Camera model : iPhone 3G
Date/Time    : 2010:11:18 18:40:28
Resolution   : 1200 x 1600
Flash used   : No
Aperture     : f/2.8
Whitebalance : Auto
Metering Mode: average
Exposure     : program (auto)
GPS Latitude : N 14d 40.54m  0s
GPS Longitude: W 17d 27.82m  0s
GPS Altitude :  53.00m

From that command I only need the following to be printed on the bottom left corner of the pictures.

GPS Latitude : N 14d 40.54m  0s
GPS Longitude: W 17d 27.82m  0s
GPS Altitude :  53.00m

---------- Post updated at 05:56 PM ---------- Previous update was at 05:54 PM ----------

Thx for your reply,
I have tested with the following error.

alioune@baccus ~/GPStagtoPic $ 
alioune@baccus ~/GPStagtoPic $ ls
001.JPG  002.JPG  003.JPG  004.JPG  006.JPG  GPStagtoPic.sh  IMG_0276.JPG  IMG_0327.JPG  ML.jpg
alioune@baccus ~/GPStagtoPic $ ./GPStagtoPic.sh 
convert -pointsize 18 -font /path/to/font.ttf -fill white -stroke black -strokewidth 1 -draw " GPS Latitude : N 14d 40.54m  0s  10,-50" -draw " Longitude:  10,-32" -draw " GPS Altitude :  53.00m   10,-14"  ML.jpg target.jpg
alioune@baccus ~/GPStagtoPic $ ls
001.JPG  002.JPG  003.JPG  004.JPG  006.JPG  GPStagtoPic.sh  IMG_0276.JPG  IMG_0327.JPG  ML.jpg
alioune@baccus ~/GPStagtoPic $ 

when I execute the imagemagick command

alioune@baccus ~/GPStagtoPic $ convert -pointsize 18 -font /path/to/font.ttf -fill white -stroke black -strokewidth 1 -draw " GPS Latitude : N 14d 40.54m  0s  10,-50" -draw " Longitude:  10,-32" -draw " GPS Altitude :  53.00m   10,-14"  ML.jpg target.jpg
convert: non-conforming drawing primitive definition `GPS' @ error/draw.c/DrawImage/3143.
convert: non-conforming drawing primitive definition `Longitude' @ error/draw.c/DrawImage/3143.
convert: non-conforming drawing primitive definition `GPS' @ error/draw.c/DrawImage/3143.
convert: non-conforming drawing primitive definition `Longitude' @ error/draw.c/DrawImage/3143.
convert: non-conforming drawing primitive definition `GPS' @ error/draw.c/DrawImage/3143.
convert: non-conforming drawing primitive definition `Longitude' @ error/draw.c/DrawImage/3143.
convert: non-conforming drawing primitive definition `GPS' @ error/draw.c/DrawImage/3143.
alioune@baccus ~/GPStagtoPic $ 

The file is generated but the wrong information is printed on it.

Looks like there were a couple of slight issues with the solution posted:

length is an awk function and should be used as a variable name
image magic draw function requires text x,y 'string'

Adjusted script to correct these issues:

source=source.jpg
target=target.JPG
# get the image's size, width=$1, length=$2, you need adjust for your image,
# depand the output format of command identify
set -- $(identify $source |awk 'NR==1{split($3,a,"x"); print a[1],a[2]}')   
 
cat jhead |grep "GPS " |awk -v width=$1 -v lngth=$2 -v sfile=$source -v tfile=$target '
NR==1{Latitude=$0}NR==2{Longitude=$2} NR==3{Altitude=$0}
END{
    printf "convert -pointsize 18 -font times.ttf -fill white "
   printf "-stroke black -strokewidth 1 "
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-72, 39, Latitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-54, 39, Longitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-36, 39, Altitude, 39
    printf "%s %s\n", sfile, tfile }'

Other things to consider: the black stroke around the text looks really poor with my testing images and you may want to try without the stroke like (remove line is green above). I always like using a gamma adjust in a rectangle where the text is to go (say 0.2 gamma with white text), this tends to help the text stand out from the background.

You would need to play around with imagemagic to measure the size of the text and darken a rectangle where it is to go.

---------- Post updated at 03:35 PM ---------- Previous update was at 01:24 PM ----------

I've had a go at creating a dark plaque sized from the GPS text. This should get you going, but it could be tweaked more.

Your GPS text may always be a consistent width and you could just use a standard image for the plaque (just get rid of the maxwidth function and the convert command that created the black plauque.jpg images:

source=source.jpg
target=target.JPG
 
# get the image's size, width=$1, length=$2, you need adjust for your image,
# depand the output format of command identify
set -- $(identify $source |awk 'NR==1{split($3,a,"x"); print a[1],a[2]}')  
I_WIDTH=$1
I_LENGTH=$2
 
function maxwidth()
{
        MAX_WIDTH=0
        while read line
        do
                INFO=$(convert -size 1025x768 xc:lightblue -font times.ttf -pointsize 18 -fill none -undercolor white -annotate +20+100 "$line" -trim info:)
                WIDTH=${INFO##*XC }
                WIDTH=${WIDTH%%x*}
        [ $WIDTH -gt $MAX_WIDTH ] && MAX_WIDTH=$WIDTH
        done
        echo $MAX_WIDTH
}
 
MW=$(cat jhead | grep "GPS " | maxwidth)
let WIDTH=MW+10
HEIGHT=60
 
convert -size ${WIDTH}x${HEIGHT} xc:black plaque.jpg
composite plaque.jpg -geometry +5+$(( $I_LENGTH - $HEIGHT - 5 )) -dissolve 60 $source $target
 
CMD=$(cat jhead |grep "GPS " |awk -v width=$1 -v lngth=$2 -v tfile=$target '
NR==1{Latitude=$0}NR==2{Longitude=$0} NR==3{Altitude=$0}
END{
    printf "convert -pointsize 18 -font times.ttf -fill white "
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-46, 39, Latitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-28, 39, Longitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-10, 39, Altitude, 39
    printf "%s %s\n", tfile, tfile }')
eval $CMD

Here is a pretty light image (mainly white where the text is to appear) and I think it is still quite readable at 60%:

2 Likes

thx again Chuble_XL it is working very well the black frame is perfect.

The script is working for a specific jpg. I would like it work for any jpg, using the following form.

GPStoPic file1.jpg # for a chosen file
or
GPStoPic for * # for all the files in a specific directory.

Once the program executed it should output append "GPS" in the name of the file.

ex: GPStoPic file1.jpg will output file1GPS.jpg

Actual scipt

source=source.jpg
target=target.JPG
 
# get the image's size, width=$1, length=$2, you need adjust for your image,
# depand the output format of command identify
set -- $(identify $source |awk 'NR==1{split($3,a,"x"); print a[1],a[2]}')  
I_WIDTH=$1
I_LENGTH=$2
 
function maxwidth()
{
        MAX_WIDTH=0
        while read line
        do
                INFO=$(convert -size 1025x768 xc:lightblue -font times.ttf -pointsize 18 -fill none -undercolor white -annotate +20+100 "$line" -trim info:)
                WIDTH=${INFO##*XC }
                WIDTH=${WIDTH%%x*}
        [ $WIDTH -gt $MAX_WIDTH ] && MAX_WIDTH=$WIDTH
        done
        echo $MAX_WIDTH
}
 
MW=$(jhead $source| grep "GPS " | maxwidth)
let WIDTH=MW+10
HEIGHT=60
 
convert -size ${WIDTH}x${HEIGHT} xc:black plaque.jpg
composite plaque.jpg -geometry +5+$(( $I_LENGTH - $HEIGHT - 5 )) -dissolve 60 $source $target
 
CMD=$(jhead $source |grep "GPS " |awk -v width=$1 -v lngth=$2 -v tfile=$target '
NR==1{Latitude=$0}NR==2{Longitude=$0} NR==3{Altitude=$0}
END{
    printf "convert -pointsize 18 -font times.ttf -fill white "
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-46, 39, Latitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-28, 39, Longitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-10, 39, Altitude, 39
    printf "%s %s\n", tfile, tfile }')
eval $CMD
#END

Can anybody help
thx again Chuble_XL

If your going to process a few images it is worth removing the calls to awk and using bash arrays/string processing instead.

Here is a version that defines font and size at to so you can trial different fonts/point sizes:

#!/bin/bash
FONT=times.ttf
FONT_SIZE=18
 
# Get width or length from image info string
function getwl
{
  read WnL
  # Get 3rd word
  WnL=${WnL#* * }
  WnL=${WnL%% *}
  # Remove from + to end
  WnL=${WnL%%+*}
  # Width before 'x' Length after
  [ $1 = 'L' ] && echo ${WnL#*x}
  [ $1 = 'W' ] && echo ${WnL%x*}
}
 
function process_image
{
    source=$1
    target=${1%.jpg}GPS.jpg
 
    I_HEIGHT=$(identify $source | getwl L)
 
    # Fetch GPS info into array
    IFS="
    " GPS=( $(jhead $source | grep "GPS ") )
 
    MW=0
    for line in "${GPS[0]}" "${GPS[1]}" "${GPS[2]}"
    do
        WIDTH=$(convert -size 1025x768 xc:lightblue -font $FONT \
            -pointsize $FONT_SIZE -fill none -undercolor white \
            -annotate +20+100 "$line" -trim info: | getwl W)
        [ $WIDTH -gt $MW ] && MW=$WIDTH
    done
 
    let WIDTH=MW+10
    let P_HEIGHT=FONT_HEIGHT*3+6
 
    convert -size ${WIDTH}x${P_HEIGHT} xc:black plaque.jpg
    composite plaque.jpg -geometry +5+$(( $I_HEIGHT - $P_HEIGHT - 5 )) \
         -dissolve 60 $source $target
 
    convert -pointsize $FONT_SIZE -font $FONT -fill white \
        -draw "text 10,$((I_HEIGHT - FONT_HEIGHT*7/3 - 8)) \"${GPS[0]}\"" \
        -draw "text 10,$((I_HEIGHT - FONT_HEIGHT*4/3 - 8)) \"${GPS[1]}\"" \
        -draw "text 10,$((I_HEIGHT - FONT_HEIGHT*1/3 - 8)) \"${GPS[2]}\"" \
        $target $target
}
 
FONT_HEIGHT=$(convert -size 1025x768 xc:lightblue -font $FONT \
            -pointsize $FONT_SIZE -fill none -undercolor white \
            -annotate +20+100 "Testing" -trim info: | getwl L)
 
for file in $@
do
   process_image $file
done
1 Like

thx, I tested it, but the frame txt is not complete.

alioune@baccus ~/GPStagtoPic $ chmod u+x GPStagtoPic.sh
alioune@baccus ~/GPStagtoPic $ ./
-bash: ./: is a directory
alioune@baccus ~/GPStagtoPic $ ./GPStagtoPic.sh source.jpg 
convert: unable to read font `times.ttf' @ warning/annotate.c/RenderType/807.
convert: unable to read font `times.ttf' @ warning/annotate.c/RenderType/807.
convert: unable to read font `times.ttf' @ warning/annotate.c/RenderType/807.
convert: unable to read font `times.ttf' @ warning/annotate.c/RenderType/807.
convert: unable to read font `times.ttf' @ warning/annotate.c/RenderType/807.
alioune@baccus ~/GPStagtoPic $ ls
GPStagtoPic.sh  GPStagtoPic.sh-old  plaque.jpg  sourceGPS.jpg  source.jp

Sorry it's because the line assigning GPS has been indented, my bad (and a poor way to set IFS anyway) for writing code that must start in column 1!

Replace:

    # Fetch GPS info into array
    IFS="
    " GPS=( $(jhead $source | grep "GPS ") )
 

with

    # Fetch GPS info into array
    IFS=$'\n' GPS=($(jhead $source | grep "GPS " ))

BTW, To avoid warnings about font not found, you should set FONT= at the top of the program to a pathname contaning your font eg FONT=/usr/share/font/misc/times.ttf or make a font directory in your home dir and copy fonts you might want to try out there FONT=~/fonts/arial.ttf

1 Like

Chulber_XL, thx for thi sversion. I tried it but all the information is not printed on the pictures. the black frame is too shorl also.

New Script - 2n version

Old Script - 1st version

If fixing is too complicated, I am satisfied with thee first versions, it works fine. How can you modify it in order to execute it on many jpg at the same time.
using this form

 ./GPStagtoPic.sh *.jpg

Old version of the script (OK)

source=$1
target=${1%.jpg}GPS.jpg

# get the image's size, width=$1, length=$2, you need adjust for your image,
# depand the output format of command identify


set -- $(identify $source |awk 'NR==1{split($3,a,"x"); print a[1],a[2]}')  

I_WIDTH=$1
I_LENGTH=$2
 
function maxwidth()
{
        MAX_WIDTH=0
        while read line
        do
                INFO=$(convert -size 1025x768 xc:lightblue -font times.ttf -pointsize 18 -fill none -undercolor white -annotate +20+100 "$line" -trim info:)
                WIDTH=${INFO##*XC }
                WIDTH=${WIDTH%%x*}
        [ $WIDTH -gt $MAX_WIDTH ] && MAX_WIDTH=$WIDTH
        done
        echo $MAX_WIDTH
}
 
 
MW=$(jhead $source| grep "GPS " | maxwidth)

let WIDTH=MW+10
HEIGHT=60
 
convert -size ${WIDTH}x${HEIGHT} xc:black plaque.jpg
composite plaque.jpg -geometry +5+$(( $I_LENGTH - $HEIGHT - 5 )) -dissolve 60 $source $target
 
CMD=$(jhead $source |grep "GPS " |awk -v width=$1 -v lngth=$2 -v tfile=$target '
NR==1{Latitude=$0}NR==2{Longitude=$0} NR==3{Altitude=$0}
END{
    printf "convert -pointsize 18 -font times.ttf -fill white "
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-46, 39, Latitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-28, 39, Longitude, 39
    printf "-draw \"text 10,%d %c%s%c\" ", lngth-10, 39, Altitude, 39
    printf "%s %s\n", tfile, tfile }')
eval $CMD

Newversion of the script (NOK)

#!/bin/bash
FONT=times.ttf
FONT_SIZE=18
 
# Get width or length from image info string
function getwl
{
  read WnL
  # Get 3rd word
  WnL=${WnL#* * }
  WnL=${WnL%% *}
  # Remove from + to end
  WnL=${WnL%%+*}
  # Width before 'x' Length after
  [ $1 = 'L' ] && echo ${WnL#*x}
  [ $1 = 'W' ] && echo ${WnL%x*}
}
 
function process_image
{
    source=$1
    target=${1%.jpg}GPS.jpg
 
    I_HEIGHT=$(identify $source | getwl L)
 
    # Fetch GPS info into array
    IFS="
    " GPS=( $(jhead $source | grep "GPS ") )
 
    MW=0
    for line in "${GPS[0]}" "${GPS[1]}" "${GPS[2]}"
    do
        WIDTH=$(convert -size 1025x768 xc:lightblue -font $FONT \
            -pointsize $FONT_SIZE -fill none -undercolor white \
            -annotate +20+100 "$line" -trim info: | getwl W)
        [ $WIDTH -gt $MW ] && MW=$WIDTH
    done
 
    let WIDTH=MW+10
    let P_HEIGHT=FONT_HEIGHT*3+6
 
    convert -size ${WIDTH}x${P_HEIGHT} xc:black plaque.jpg
    composite plaque.jpg -geometry +5+$(( $I_HEIGHT - $P_HEIGHT - 5 )) \
         -dissolve 60 $source $target
 
    convert -pointsize $FONT_SIZE -font $FONT -fill white \
        -draw "text 10,$((I_HEIGHT - FONT_HEIGHT*7/3 - 8)) \"${GPS[0]}\"" \
        -draw "text 10,$((I_HEIGHT - FONT_HEIGHT*4/3 - 8)) \"${GPS[1]}\"" \
        -draw "text 10,$((I_HEIGHT - FONT_HEIGHT*1/3 - 8)) \"${GPS[2]}\"" \
        $target $target
}
 
FONT_HEIGHT=$(convert -size 1025x768 xc:lightblue -font $FONT \
            -pointsize $FONT_SIZE -fill none -undercolor white \
            -annotate +20+100 "Testing" -trim info: | getwl L)
 
for file in $@
do
   process_image $file
done

Thx,

See my previous post (#9), you still have the old IFS= code in your Newversion script.

Tested and working. Thx again. your help is much aprciated..