I tried to use on my MacBook a script I found that does 1. changes some filters of an imported image and 2. changes the metadata of it. After adjusting a bit it worked but the metadata did not change using the parameters from the script as you can see. (On the left is the original image and on the right is the exported one by the script). My point is, how can the data that is written on the script be applied? (a random device name, a random creation date etc). Any other improvement is welcome please.
The script code i used:
import os
import random
import shutil
from PIL import Image, ImageEnhance, ImageFilter
import piexif
import datetime
def clean_photo(photo_path, output_path):
# Load the photo
photo = Image.open(photo_path)
if "exif" in photo.info:
exif_dict = piexif.load(photo.info["exif"])
exif_dict.clear()
exif_bytes = piexif.dump(exif_dict)
photo_info = photo.info.copy()
photo_info["exif"] = exif_bytes
else:
photo_info = photo.info.copy()
photo.info["Location"] = "United States"
exposure = random.randint(-2, 2)
brilliance = random.randint(-2, 2)
highlights = random.randint(-2, 2)
contrast = random.randint(-2, 2)
brightness = random.randint(-2, 2)
saturation = random.randint(-2, 2)
vibrancy = random.randint(-2, 2)
margin_pixels = random.randint(5, 20) # Number of pixels to cut from each margin
width, height = photo.size
left = margin_pixels
upper = margin_pixels
right = width - margin_pixels
lower = height - margin_pixels
photo = photo.crop((left, upper, right, lower))
photo = photo.filter(ImageFilter.GaussianBlur(radius=random.randint(1, 3)))
camera_make = random.choice(["Canon", "Nikon", "Sony", "Fuji"])
camera_model = random.choice(["Model A", "Model B", "Model C"])
if "exif" in photo.info:
exif_dict = piexif.load(photo.info["exif"])
exif_dict["0th"][piexif.ImageIFD.Make] = camera_make
exif_dict["0th"][piexif.ImageIFD.Model] = camera_model
else:
exif_dict = {"0th": {piexif.ImageIFD.Make: camera_make, piexif.ImageIFD.Model: camera_model}}
exif_bytes = piexif.dump(exif_dict)
photo_info["exif"] = exif_bytes
random_date = datetime.datetime(
random.randint(2000, 2021),
random.randint(1, 12),
random.randint(1, 28),
random.randint(0, 23),
random.randint(0, 59),
random.randint(0, 59)
)
creation_date = random_date.strftime("%Y:%m:%d %H:%M:%S")
if "exif" in photo_info:
exif_dict = piexif.load(photo_info["exif"])
exif_dict["0th"][piexif.ImageIFD.DateTime] = creation_date.encode("utf-8")
else:
exif_dict = {"0th": {piexif.ImageIFD.DateTime: creation_date.encode("utf-8")}}
exif_bytes = piexif.dump(exif_dict)
photo_info["exif"] = exif_bytes
random_filename = ''.join(random.choices('01234567890', k=10))
filename, ext = os.path.splitext(photo_path)
output_filename = f"IMG_{random_filename}{ext}"
output_file_path = os.path.join(output_path, output_filename)
photo.save(output_file_path)
def clean_photos(main_folder, usedpics_folder, num_iterations):
output_folder = os.path.join(main_folder, "CleanedPics")
os.makedirs(output_folder, exist_ok=True)
usedpics_path = os.path.join(main_folder, usedpics_folder)
usedpics = os.listdir(usedpics_path)
for i in range(num_iterations):
iteration_folder = os.path.join(main_folder, f"Iteration_{i+1}")
os.makedirs(iteration_folder, exist_ok=True)
for usedpic in usedpics:
usedpic_path = os.path.join(usedpics_path, usedpic)
clean_photo(usedpic_path, iteration_folder)
main_folder = "/Users/macpro/Downloads/script”
usedpics_folder = “used”
num_iterations = 5
clean_photos(main_folder, usedpics_folder, num_iterations)