デジカメ写真に日付を表示

昔、デジカメが未だ存在しなかった頃、写真の撮影日をフィルムに直接焼き付けてくれるカメラがありました。当時は、どのような方法を使っていたのかわからなかったのですが、現在ならImageMagickのconvertコマンドと、jheadによるEXIF情報の取得により、簡単にデジカメ写真に日付を挿入することが可能となります。

f:id:S_E_Hyphen:20180213104514j:plain

 

 

#!/bin/bash
sdoc=\
" InsertDate  - JPEGEXIF情報から撮影日を取得し、    \n"\
"                写真右下隅に表示する                  \n"\
"                                                      \n"\
" [ require]                                           \n"\
"    in          入力のJPGファイル                     \n"\
"                                                      \n"\
" [ option ]                                           \n"\
"   foreground  日付の文字色      規定値はwhite       \n"\
"   background  日付の背景色      規定値はnone        \n"\
"                                                      \n"\
" [ 出力 ]                                            \n"\
"    InsertDate in=xxxx.jpg を実行すると               \n"\
"    xxxx_inserted_date.jpg が出力される               \n"\
"    出力されるJPEGファイルの横幅は1000pixelに統一する \n"\
"                                                      \n"
export sdoc

in=`getparstr $# "$*" "in"`
foreground=`getparstr $# "$*" "foreground"`
background=`getparstr $# "$*" "background"`
if [ -z $in ]; then usage;exit; fi
if [ -z $foreground ]
then 
 foreground='#FFFFFF'
fi
if [ -z $background ]
then 
 background='none'
fi

temp=`mktemp ./XXXX.jpg`
convert -resize 1000x1000 ${in} $temp # 1000ピクセルに正規化
OutputJpegFile=`basename ${in} .JPG`_inserted_date.jpg

height=`jhead $temp | grep Resolution | awk '{print $5}'`
width=`jhead $temp | grep Resolution | awk '{print $3}'`
date=`jhead ${in} | grep Date\/Time | awk '{print $3}'| sed -e "s/:/-/g"`

x0=865
x1=860
x2=960
y0=`expr $height - 15`
y1=`expr $height - 10`
y2=`expr $height - 30`

# フォントはTimes-Roman,20pt
# 背景色で長方形を塗りつぶす(880,-10)-(980,-30)
# (885,-15)を左中央として日付を描画
convert -font Times-Roman -pointsize 20 \
-fill ${background} -draw "rectangle $x1,$y1 $x2,$y2" \
-fill ${foreground} -annotate +${x0}+${y0} "${date}"  \
$temp $OutputJpegFile
rm $temp