Untitled
unknown
plain_text
a year ago
1.4 kB
19
Indexable
def add_meta_data_to_image(filepath, heading):
statement = create_statement(heading)
try:
# Open the image file
with Image.open(filepath) as img:
# Check if the image has EXIF data
exif_data = img.getexif()
if exif_data:
# Load existing EXIF data
exif_dict = piexif.load(exif_data.tobytes())
else:
# Create a new EXIF dictionary
exif_dict = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}}
# Set custom metadata
exif_dict['0th'][piexif.ImageIFD.Copyright] = f"This image belongs to {site_name}. This image is for explaining '{heading}' and should not be used without permission of {site_name}."
exif_dict['0th'][piexif.ImageIFD.ImageDescription] = f"{statement}"
exif_dict['0th'][piexif.ImageIFD.Rating] = 5
if author_owner:
exif_dict['0th'][piexif.ImageIFD.Artist] = f"{author_owner}"
# Convert the EXIF dictionary to bytes
exif_bytes = piexif.dump(exif_dict)
# Save the image with the new EXIF data
img.save(filepath, "jpeg", exif=exif_bytes)
print(f"Image processed and saved with metadata: {filepath}")
except Exception as e:
print(f"Error processing image: {e}")Editor is loading...
Leave a Comment