Skip to content

Instantly share code, notes, and snippets.

@Whats-A-MattR
Last active March 11, 2023 07:54
Show Gist options
  • Save Whats-A-MattR/3d9313f88004a00cbb09f4469212e650 to your computer and use it in GitHub Desktop.
Save Whats-A-MattR/3d9313f88004a00cbb09f4469212e650 to your computer and use it in GitHub Desktop.
Handy script to cleanse a exif data from an image, writing to a new file to avoid any missed properties
from PIL import Image
import os
import math
import multiprocessing
import threading
cpu_strength = int(math.ceil(multiprocessing.cpu_count() / 2))
if cpu_strength == 0: cpu_strength = 1
img_path = str(input(
'please input path to image or folder containing images: (default="current directory") ') or '.')
suffix = str(input('cleaned file suffix: (default="_clean")') or "_clean")
accepted_exts = ['.jpg', '.jpeg', '.webp', '.png']
def scrub(path):
fn = os.path.split(path)[-1]
dir = os.path.abspath(os.path.join(path, os.pardir))
img = Image.open(os.path.join(dir, fn))
img_contents = list(img.getdata())
clean_image = Image.new(img.mode, img.size)
clean_image.putdata(img_contents)
filename = set_filename(fn)
clean_image.save(os.path.join(dir, filename))
print(f'Cleaned Image Saved to {os.path.join(dir, filename)}')
def set_filename(path):
p_list = list(os.path.splitext(path))
p_list.insert((len(p_list) - 1), suffix)
return ''.join(p_list)
if os.path.isfile(img_path) and os.path.splitext(img_path)[1] in accepted_exts:
path = os.path.abspath(img_path)
scrub(path)
if os.path.isdir(img_path):
dir = os.path.abspath(img_path)
files = []
threads = []
for path in os.scandir(dir):
if path.is_file() and os.path.splitext(path.name)[1] in accepted_exts:
files.append(os.path.abspath(path))
for i in range(len(files)):
t = threading.Thread(target=scrub, args=(files[i],))
threads.append(t)
for thread in threads:
thread.start()
thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment