カメラの画像フォルダ名は連番で付加されますが、同じメーカーのカメラを複数使っていると、そのうち衝突してしまう恐れがあります。そこで、画像フォルダ名の末尾に撮影年を付加することにしました。たとえば、906MSDCF_2016のようにです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python3 | |
from PIL import Image | |
from datetime import datetime | |
imgname = input(); | |
img = Image.open(imgname) | |
#DateTimeOriginal のExif tag ID | |
taken_datetime_exif = img._getexif()[0x9003] | |
taken_datetime = datetime.strptime(taken_datetime_exif, '%Y:%m:%d %H:%M:%S') | |
print(taken_datetime.strftime('%Y'), end='') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cat renamedirs.txt | while read DIRN | |
do | |
FILENAME=`ls -1 $DIRN | sed -n '1p'` | |
YEAR=`echo ${DIRN}/${FILENAME} | python3 photodir_year.py` | |
echo "renaming ${DIRN}" | |
mv $DIRN ${DIRN}_${YEAR} | |
done |
ls -1
の結果のうち反映させたいディレクトリをrenamedirs.txtに入れて、実行すると動きます。うまく動かなかったものについては手で修正する前提なので、例外処理などはしていません。