#!/bin/bash # Usage: jpg_2_metadata.sh FILE # # Dependencies: # # * identify (ImageMagick or GraphicsMagick) # # * exiftool # # * units (GNU units) # # * MagneticField (geographiclib-tools package) # # * wmm dataset for MagneticField to work (installed with # geographiclib-get-magnetic from geographiclib-tools package) imageurl="$1" image="${imageurl##*/}" SVGMIDDLE=800 printf "%s\n" "$1" >&2 ## 1) Printing metadata from the JPEG file printf "%s\n" "Metadata for the picture $image" printf "%s\n" "identify -verbose $imageurl" printf "%s\n" "--- BEGIN JPEG METADATA ---" identify -verbose "$imageurl" printf "%s\n" "--- END JPEG METADATA ---" printf "%s\n" "exiftool $imageurl" printf "%s\n" "--- BEGIN JPEG METADATA ---" exiftool "$imageurl" printf "%s\n" "--- END JPEG METADATA ---" ## 2) Calculating geographical position # exiflatituderef=$(identify -format '%[exif:GPSLatitudeRef]\n' "$imageurl" 2>/dev/null) exiflatituderef=$(exiftool -n '-exif:GPSLatitudeRef' "$imageurl" 2>/dev/null) if [ "$exiflatituderef" == "N" ]; then # latituderef='+' latituderef='' elif [ "$exiflatituderef" == "S" ]; then latituderef='-' else printf "%s\n" "No GPS metadata, exiting" >&2; exit 0 fi # exiflongituderef=$(identify -format '%[exif:GPSLongitudeRef]\n' "$imageurl" 2>/dev/null) exiflongituderef=$(exiftool -n '-exif:GPSLongitudeRef' "$imageurl" 2>/dev/null) if [ "$exiflongituderef" == "E" ]; then # longituderef='+' longituderef='' elif [ "$exiflongituderef" == "W" ]; then longituderef='-' else printf "%s\n" "No GPS metadata, exiting" >&2; exit 0 fi # exifaltituderef=$(identify -format '%[exif:GPSAltitudeRef]\n' "$imageurl" 2>/dev/null) exifaltituderef=$(exiftool -n '-exif:GPSAltitudeRef' "$imageurl" 2>/dev/null) if [ "$exifaltituderef" == "0" ]; then # altituderef='+' altituderef='' elif [ "$exifaltituderef" == "1" ]; then altituderef='-' else printf "%s\n" "No GPS metadata, exiting" >&2; exit 0 fi # exiflatitude=$(identify -format '%[exif:GPSLatitude]\n' "$imageurl") # exiflatitude_d=$(echo $exiflatitude | cut -d"," -f1 | bc) # exiflatitude_m=$(echo $exiflatitude | cut -d"," -f2 | bc) # exiflatitude_s=$( echo "scale=10 ; $(echo $exiflatitude | cut -d"," -f3)" | bc) # latitudenum=$(units "$exiflatitude_d deg + $exiflatitude_m arcmin + $exiflatitude_s arcsec" deg | head -n1 | cut -d' ' -f2) latitudenum=$(exiftool -n '-exif:GPSLatitude' "$imageurl") latitude=${latituderef}${latitudenum} # exiflongitude=$(identify -format '%[exif:GPSLongitude]\n' "$imageurl") # exiflongitude_d=$(echo $exiflongitude | cut -d"," -f1 | bc) # exiflongitude_m=$(echo $exiflongitude | cut -d"," -f2 | bc) # exiflongitude_s=$( echo "scale=10 ; $(echo $exiflongitude | cut -d"," -f3)" | bc) # longitudenum=$(units "$exiflongitude_d deg + $exiflongitude_m arcmin + $exiflongitude_s arcsec" deg | head -n1 | cut -d' ' -f2) longitudenum=$(exiftool -n '-exif:GPSLongitude' "$imageurl") longitude=${longituderef}${longitudenum} # exifaltitude=$(identify -format '%[exif:GPSAltitude]\n' "$imageurl") # altitudenum=$(echo "scale=1 ; $exifaltitude" | bc) altitudenum=$(exiftool -n '-exif:GPSAltitude' "$imageurl") altitude=${altituderef}${altitudenum} ## 3) Calculating camera direction # exifdirectionref=$(identify -format '%[exif:GPSImgDirectionRef]\n' "$imageurl" 2>/dev/null) exifdirectionref=$(exiftool -n '-exif:GPSImgDirectionRef' "$imageurl" 2>/dev/null) if [ "$exifdirectionref" == "T" ]; then directionref=0 elif [ "$exifdirectionref" == "M" ]; then d=$(date -I) directionref=$(echo ${d} ${latitude} ${longitude} ${altitude} | MagneticField | cut -d' ' -f 1) else printf "%s\n" "No GPS (compass) metadata" >&2; fi # exifdirection=$(identify -format '%[exif:GPSImgDirection]\n' "$imageurl") exifdirection=$(exiftool -n '-exif:GPSImgDirection' "$imageurl") direction=$(echo "scale=2 ; ${directionref} + ${exifdirection}" | bc) # convert degrees into radians and substract 90° # (we turn clockwise starting from x axis) # this is not the standard polar frame directionrad=$(echo "scale=4 ; ${direction} * 2 * 3.14159/360 - 3.14159/2" | bc) ## 3.b) Calculating camera direction segment for svg x1=${SVGMIDDLE} y1=${SVGMIDDLE} x2=$(echo "scale=4 ; ${x1} + ${SVGMIDDLE} * c(${directionrad})" | bc -l) y2=$(echo "scale=4 ; ${y1} + ${SVGMIDDLE} * s(${directionrad})" | bc -l) ## 4) Calculating camera angle photoangledeg2=$(exiftool -FOV "$imageurl" | awk '{ print $5 }') photoanglerad2=$(echo "scale=4 ; ${photoangledeg2} * 2 * 3.14159 / 360" | bc) # ################# # # obsolete code # # exiffocallength=$(identify -format '%[exif:FocalLength]\n' "$imageurl") # focal=$(echo $exiffocallength | cut -d'/' -f1) # # The angle here has been measured by shooting a coordinate system # # from a known distance, at different zoom levels # if [ $focal -eq 450 ]; then # photoanglerad="0.4734" ; # elif [ $focal -eq 1800 ]; then # photoanglerad="0.1636" ; # elif [ $focal -gt 450 ] && [ $focal -lt 1800 ]; then # # The formula has been determined by applying a non-linear # # regression to recorded data # photoanglerad=$(echo "0.0000001545 * ${focal}^2 + -0.0005740 * ${focal} + 0.6983" | bc) ; # else # photoanglerad="null" && printf "%s\n" "focal = $focal : unsupported case" >&2; # fi # photoanglerad=$(echo "2*${photoanglerad}" | bc) # photoangledeg=$(echo "scale=4 ; ${photoanglerad} * 180 / 3.14159" | bc) # ################# ## 4.b) Calculating camera angle segments for svg if [ ${photoanglerad2} != "null" ]; then # get the direction of the two lines # used to represent the camera angle dirangle1rad=$(echo "scale=4 ; ${directionrad} - 0.5*${photoanglerad2}" | bc) dirangle2rad=$(echo "scale=4 ; ${directionrad} + 0.5*${photoanglerad2}" | bc) x3=$(echo "scale=4 ; ${x1} + ${SVGMIDDLE} * c(${dirangle1rad})" | bc -l) y3=$(echo "scale=4 ; ${y1} + ${SVGMIDDLE} * s(${dirangle1rad})" | bc -l) x4=$(echo "scale=4 ; ${x1} + ${SVGMIDDLE} * c(${dirangle2rad})" | bc -l) y4=$(echo "scale=4 ; ${y1} + ${SVGMIDDLE} * s(${dirangle2rad})" | bc -l) else printf "%s\n" "Won't calculate camera angle" >&2; fi osmurl="https://www.openstreetmap.org/#map=19/${latitude}/${longitude}" geourl="geo:${latitude},${longitude},${altitude}" printf "\n%s\n" "OSM URL: $osmurl" printf "\n%s\n" "GEO URL: $geourl" printf "\n%s\n" "Direction (clockwise angle from true North):" printf "%s\n" "degrees: $direction" printf "\n%s\n" "Photo angle:" printf "%s\n" "degrees: $photoangledeg" printf "%s\n" "radians: $photoanglerad" printf "%s\n" "degrees: $photoangledeg2" printf "%s\n" "radians: $photoanglerad2" printf "\n%s\n" "A,B,C,D positions for svg generation:" printf "%s\n" "A: ${x1} ${y1}" printf "%s\n" "B: ${x2} ${y2}" printf "%s\n" "C: ${x3} ${y3}" printf "%s\n" "D: ${x4} ${y4}"