技术提示:向照片添加经纬度信息
于 2009 年 12 月 10 日
我想将地理位置信息存储在我用数码相机拍摄的照片中。 这样,当我将照片上传到 Picasa 网页时,就不必手动指定照片位置。 由于我的相机没有内置 GPS 支持,因此我编写了这个脚本,以便在照片已经导入电脑后,将位置信息添加到照片中。
该脚本可以与 Dave Taylor 的 whereis.sh 脚本 一起使用,该脚本在此 文章中描述。 Dave 的脚本接受一个地址,并使用 Yahoo Maps 服务返回经纬度值。 我的脚本 - 我称之为jpgloc.sh- 可以通过管道传递来自whereis.sh的输出,例如
sh whereis.sh 2001 Blake Street, Denver, CO | ./jpgloc.sh
# OR
sh whereis.sh 2001 Blake Street, Denver, CO | sh jpgloc.sh
它从标准输入中获取两个值。 将它们转换为度/分格式,并确定它是北/南纬还是东/西经。 之后,它循环遍历当前目录中的 JPEG 图像,并将 GPS 纬度/经度值设置为提供的位置。 它使用exiv2实用程序进行最后一步。 该程序应该可以通过软件包管理系统用于最新的发行版。 例如,在 Ubuntu Linux 上,可以使用以下命令安装它
sudo apt-get install exiv2
如果纬度/经度数据以其他格式提供,则可以轻松修改/扩展该脚本以从命令行参数中获取相同的信息。 该脚本的源代码如下
#!/bin/sh
# Grabs latitude and longitude values from stdin and stores
# them in all JPGs in current directory.
# The two values have to be separated by a comma.
#
IFS=","
read lat long
# If sign is negative, we have to change reference
latRef="N";
latSign=$(echo $lat | cut -c 1)
if [ "X$latSign" = "X-" ]
then # Delete minus from beginning
lat=$(echo $lat | sed s/^-//)
latRef="S"
fi
lonRef="E";
lonSign=$(echo $long | cut -c 1)
if [ "X$lonSign" = "X-" ]
then
long=$(echo $long | sed s/^-//)
lonRef="W"
fi
# Calculate latitude/longitude degrees and minutes
latDeg=$( echo "scale=0; $lat/1" | bc )
latMin=$( echo "scale=2; m=($lat-$latDeg) *100/ 0.016666;scale=0;m/1" | bc )
lonDeg=$( echo "scale=0; $long/1" | bc )
lonMin=$( echo "scale=2; m=($long-$lonDeg)*100/0.016666;scale=0;m/1" | bc )
echo Picture location will be: $latDeg $latMin\' $latRef , $lonDeg $lonMin\' $lonRef
for fil in *.jpg # Also try *.JPG
do
echo Updating $fil ....
exiv2 -M"set Exif.GPSInfo.GPSLatitude $latDeg/1 $latMin/100 0/1" \
-M"set Exif.GPSInfo.GPSLatitudeRef $latRef" \
-M"set Exif.GPSInfo.GPSLongitude $lonDeg/1 $lonMin/100 0/1" \
-M"set Exif.GPSInfo.GPSLongitudeRef $lonRef" \
$fil
done