os」タグアーカイブ

Pythonで画像のExif情報を削除する

指定したファイルまたはディレクトリ以下のファイルのExif情報を削除します。

ソースコード1
指定したファイルのExifを削除する

from PIL import Image
from PIL.ExifTags import TAGS
import os

def delete_exif(filename):
image = Image.open(filename)
image_data = image.getdata()
image_mode = image.mode
image_size = image.size

new_image = Image.new(image_mode,image_size)
new_image.putdata(image_data)
old_filename = os.path.splitext(filename)[0]
old_ext = os.path.splitext(filename)[1][1:]
new_file = os.path.join(filename,f"{old_filename}_noexif.{old_ext}")
new_image.save(new_file)
print(f" New file > {new_file}")

def main():
filename = input("path > ")
delete_exif(filename)

return

if __name__ == "__main__":
main()

ソースコード2
指定したディレクトリ以下の「jpg/jpeg」、「png」画像ファイルのExif情報を削除する

from PIL import Image
import os

def main():
print("Exif delete tool")
root_path = input("Directory path > ")

print("Searching...")
for root,dirs,files in os.walk(root_path):
for file in files:
path = os.path.join(root,file)
ext = os.path.splitext(file)[1][1:]
target_ext = ["jpg","jpeg","png"]

if ext in target_ext:
print(f" file > {file}")
image = Image.open(path)
image_data = image.getdata()
image_mode = image.mode
image_size = image.size

new_image = Image.new(image_mode,image_size)
new_image.putdata(image_data)
old_filename = os.path.splitext(file)[0]
old_ext = os.path.splitext(file)[1][1:]
new_file = os.path.join(root,f"{old_filename}_noexif.{old_ext}")
new_image.save(new_file)
print(f" New file > {new_file}")

input("END")
return

if __name__ == "__main__":
main()

Pythonで再帰的にファイル名を取得する

再帰的にファイル名を取得する方法を以下のソースコードで説明します。
まずテスト用として、カレントディレクトリに下記の構造でテストディレクトリを生成しています。

example
├─dir_0
│ file_0
│ file_1
│ file_2
│ file_3
│ file_4
│ file_5
│ file_6
│ file_7
│ file_8
│ file_9

├─dir_1
│ file_0
│ file_1
│ file_2
│ file_3
│ file_4
│ file_5
│ file_6
│ file_7
│ file_8
│ file_9

├─dir_2
│ file_0
│ file_1
│ file_2
│ file_3
│ file_4
│ file_5
│ file_6
│ file_7
│ file_8
│ file_9

├─dir_3
│ file_0
│ file_1
│ file_2
│ file_3
│ file_4
│ file_5
│ file_6
│ file_7
│ file_8
│ file_9

└─dir_4
file_0
file_1
file_2
file_3
file_4
file_5
file_6
file_7
file_8
file_9

その後、os.walk()関数で生成したディレクトリ、ファイルと同様のパスが取得できているか確認をしています。

ソースコード

# walk.py

import os
import shutil

def make_test_files(path):
# ファイルを10個生成
for i in range(10):
filename = os.path.join(path,f"file_{i}")
fd = open(filename,"w")
fd.close()

return

def make_test_dirs():
root_dir = "example"

# ディレクトリが存在した場合は、削除する(mkdirエラー対策)
if os.path.exists(root_dir):
shutil.rmtree(root_dir)

os.mkdir(root_dir)

# ディレクトリを5個生成
for i in range(5):
sub_dir = os.path.join(root_dir,f"dir_{i}")
if os.path.exists(sub_dir) == False:
os.makedirs(sub_dir)
make_test_files(sub_dir)

return

def main():
# カレントディレクトリにテスト用ファイルを生成
make_test_dirs()

# カレントディレクトリのファイル一覧を表示
root_path = "."
for root,dirs,files in os.walk(root_path):
for file in files:
path = os.path.join(root,file)
print(path)

return

if __name__ == "__main__":
main()

実行と結果

python --version
Python 3.12.2
python walk.py
.\walk.py
.\example\dir_0\file_0
.\example\dir_0\file_1
.\example\dir_0\file_2
.\example\dir_0\file_3
.\example\dir_0\file_4
.\example\dir_0\file_5
.\example\dir_0\file_6
.\example\dir_0\file_7
.\example\dir_0\file_8
.\example\dir_0\file_9
.\example\dir_1\file_0
.\example\dir_1\file_1
.\example\dir_1\file_2
.\example\dir_1\file_3
.\example\dir_1\file_4
.\example\dir_1\file_5
.\example\dir_1\file_6
.\example\dir_1\file_7
.\example\dir_1\file_8
.\example\dir_1\file_9
.\example\dir_2\file_0
.\example\dir_2\file_1
.\example\dir_2\file_2
.\example\dir_2\file_3
.\example\dir_2\file_4
.\example\dir_2\file_5
.\example\dir_2\file_6
.\example\dir_2\file_7
.\example\dir_2\file_8
.\example\dir_2\file_9
.\example\dir_3\file_0
.\example\dir_3\file_1
.\example\dir_3\file_2
.\example\dir_3\file_3
.\example\dir_3\file_4
.\example\dir_3\file_5
.\example\dir_3\file_6
.\example\dir_3\file_7
.\example\dir_3\file_8
.\example\dir_3\file_9
.\example\dir_4\file_0
.\example\dir_4\file_1
.\example\dir_4\file_2
.\example\dir_4\file_3
.\example\dir_4\file_4
.\example\dir_4\file_5
.\example\dir_4\file_6
.\example\dir_4\file_7
.\example\dir_4\file_8
.\example\dir_4\file_9