0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

piexifを使用してExifの日付を修正した

0
Posted at

カメラの設定をミスった

すごく久々に旅行へ行ったのでデジカメを持ち出したのですが、2026年なのに日付を2025年で設定していました。帰ってきて写真をチェックしてきづいたのですが、まとめて変更できるようなアプリが見当たらなかったのでPythonでやってみました。

piexifでexifの変更

Pythonでのexifを扱うにはPIL(Pillow)で扱う例と、piexifで扱う例をみつけたのですが、ファイルの出力をPillowで実施して別ファイルに保存するものしかみつけられませんでした。その際に画像の再圧縮をしていそうなので、piexifでファイルを直接書き換える方法を調べて使用しております。

datemod.py
from PIL import Image
import piexif
import glob

def convert_date(file_name):
    print("Processing:", file_name)
    im = Image.open(file_name)
    exif_dict = piexif.load(im.info["exif"])

    for ifd in ("0th", "Exif", "GPS", "1st"):
        for tag in exif_dict[ifd]:
            tag_name = piexif.TAGS[ifd][tag]["name"]
            value = exif_dict[ifd][tag]
            if tag_name in ["DateTime", "DateTimeOriginal", "DateTimeDigitized"]:
                print(tag_name, value)
                exif_dict[ifd][tag] = value.replace(b"2025", b"2026")

    exif_bytes = piexif.dump(exif_dict)
    piexif.insert(exif_bytes, file_name) 




file_paths = glob.glob('*.JPG')

for file_path in file_paths:
    convert_date(file_path)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?