ubuntu下修改照片的EXIF信息
本文可自由转载,但请遵循“署名-非商业用途-保持一致”的创作共用协议。 永久链接:JoeCen's 小猫窝-----------------------------
这次旅行拿了两台相机,回来后才发现,其中一台的时间慢了1个多小时,这样直接导致我将两台相机的照片放在一起的时候会出现排序混乱的情况。解决方法是必须修改照片的EXIF信息,并重新命名照片的文件名。
整理的需求如下:
1、需要将时间慢了的相机所照的照片进行EXIF信息修改;
2、使用拍摄时间定义照片的名字;
2、使用拍摄时间定义照片的名字;
在windows下有些小软件可以做这件事情,不过我还是觉得在linux下使用脚本比较方便。
ubuntu下查看和修改EXIF的命令就是exif,使用方法如下:
查看EXIF信息:
exif filename
会看到很详细的EXIF信息,其中有两个:
.......
Date and Time (origi|2011:11:12 11:51:47
Date and Time (digit|2011:11:12 11:51:46
......
Date and Time (origi|2011:11:12 11:51:47
Date and Time (digit|2011:11:12 11:51:46
......
这两个应该就是拍摄时间,查看一下ids:
exif -i filename
......
0x9003|2011:11:12 11:51:47
0x9004|2011:11:12 11:51:46
......
0x9003|2011:11:12 11:51:47
0x9004|2011:11:12 11:51:46
......
修改方法:
exif -t 0x9003 --ifd=EXIF --set-value="2011:11:12 10:21:47" -o new_filename org_filename
这样就可以将0x9003的值修改为设定的值。其他用法可以man。
写了个脚本去完成我的需求:
#!/bin/sh
if [ $# -lt 1 ];then
echo "usag: $0 JPG_DIR"
exit 1
fi
output_dir="modified"
mkdir ${output_dir}
for org_file in $(ls ${1}/*.JPG)
do
#检查原来的时间
old_time=$(date -d "$(exif -t 0x9003 --ifd=EXIF ${org_file} |awk -F: '/Value/{print $2"-"$3"-"$4":"$5":"$6}')" +%s)
#计算新的时间
new_time=$(date -d @$(expr ${old_time} - 5400) "+%Y:%m:%d %H:%M:%S")
#新的文件名
new_file=$(date -d @$(expr ${old_time} - 5400) "+%Y_%m_%d_%H_%M_%S")
#输出文件名,使用Random的原因是可能有照片在同一秒拍摄
output_file="${output_dir}/${new_file}_${RANDOM}.JPG"
#对照片的EXIF进行第一次修改
exif -t 0x9003 --ifd=EXIF --set-value="$new_time" -o ${output_file} ${org_file}
old_time=$(date -d "$(exif -t 0x9004 --ifd=EXIF ${output_file} |awk -F: '/Value/{print $2"-"$3"-"$4":"$5":"$6}')" +%s)
new_time=$(date -d @$(expr ${old_time} - 5400) "+%Y:%m:%d %H:%M:%S")
#对照片的EXIF进行第二次修改
exif -t 0x9004 --ifd=EXIF --set-value="$new_time" -o ${output_file} ${output_file}
done
if [ $# -lt 1 ];then
echo "usag: $0 JPG_DIR"
exit 1
fi
output_dir="modified"
mkdir ${output_dir}
for org_file in $(ls ${1}/*.JPG)
do
#检查原来的时间
old_time=$(date -d "$(exif -t 0x9003 --ifd=EXIF ${org_file} |awk -F: '/Value/{print $2"-"$3"-"$4":"$5":"$6}')" +%s)
#计算新的时间
new_time=$(date -d @$(expr ${old_time} - 5400) "+%Y:%m:%d %H:%M:%S")
#新的文件名
new_file=$(date -d @$(expr ${old_time} - 5400) "+%Y_%m_%d_%H_%M_%S")
#输出文件名,使用Random的原因是可能有照片在同一秒拍摄
output_file="${output_dir}/${new_file}_${RANDOM}.JPG"
#对照片的EXIF进行第一次修改
exif -t 0x9003 --ifd=EXIF --set-value="$new_time" -o ${output_file} ${org_file}
old_time=$(date -d "$(exif -t 0x9004 --ifd=EXIF ${output_file} |awk -F: '/Value/{print $2"-"$3"-"$4":"$5":"$6}')" +%s)
new_time=$(date -d @$(expr ${old_time} - 5400) "+%Y:%m:%d %H:%M:%S")
#对照片的EXIF进行第二次修改
exif -t 0x9004 --ifd=EXIF --set-value="$new_time" -o ${output_file} ${output_file}
done