You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromPILimportImagedefadd_metadata(image_path, metadata):
img=Image.open(image_path)
# Convert metadata dictionary to a stringmetadata_string="\n".join([f"{key}: {value}"forkey, valueinmetadata.items()])
# Add metadata to the imageimg.info["metadata"] =metadata_string# Save the image with the new metadataimg.save("output_image.jpg")
# Example usageimage_path="input_image.jpg"metadata= {
"Title": "Your Image Title",
"Artist": "Your Name",
"License": "CC BY 4.0 - https://creativecommons.org/licenses/by/4.0/",
# Add other relevant metadata fields
}
add_metadata(image_path, metadata)
Using piexif:
importpiexiffromPILimportImagedefadd_copyright_to_exif(input_image_path, output_image_path, copyright_text):
# Open the image using Pillow (PIL)image=Image.open(input_image_path)
# Get the Exif data from the imageexif_dict=piexif.load(image.info["exif"])
# Set the copyright information in the Exif dataexif_dict["0th"][piexif.ImageIFD.Artist] =copyright_text# Save the modified Exif data back to the imageexif_bytes=piexif.dump(exif_dict)
image.save(output_image_path, exif=exif_bytes)
# Example usageinput_image_path="input.jpg"output_image_path="output.jpg"copyright_text="Your Copyright Text Here"add_copyright_to_exif(input_image_path, output_image_path, copyright_text)
Using iptcinfo3:
fromPILimportImageimportiptcinfo3defadd_iptc_metadata(image_path, metadata):
# Open the imageimg=Image.open(image_path)
# Open the IPTC infoinfo=iptcinfo3.IPTCInfo(image_path, force=True)
# Update the IPTC info with metadataforkey, valueinmetadata.items():
info[key] =value# Save the IPTC info back to the imageinfo.save_as(image_path)
# Example usageimage_path="your_image.jpg"metadata= {
(2, 5): "Your Image Title",
(2, 80): "Your Name",
(2, 116): "CC BY 4.0 - https://creativecommons.org/licenses/by/4.0/",
# Add other relevant IPTC metadata fields
}
add_iptc_metadata(image_path, metadata)
In Django would look like:
classYourImageModel(models.Model):
image=models.ImageField(upload_to='your_image_directory')
defadd_iptc_metadata(self, metadata):
# Open the image# Open the IPTC info# Update the IPTC info with metadata# Save the IPTC info back to the image# Example usage in a Django viewdefupload_image(request):
ifrequest.method=='POST':
form=YourImageForm(request.POST, request.FILES)
ifform.is_valid():
# Save the imageimage_instance=form.save()
# Add IPTC metadata to the imagemetadata= {
(2, 5): "Your Image Title",
(2, 80): "Your Name",
(2, 116): "CC BY 4.0 - https://creativecommons.org/licenses/by/4.0/",
# Add other relevant IPTC metadata fields
}
image_instance.add_iptc_metadata(metadata)
returnredirect('success_page')
else:
form=YourImageForm()
returnrender(request, 'upload_image.html', {'form': form})
CC BY 4.0 Deed (https://creativecommons.org/licenses/by/4.0/)
Using EXIF and/or IPTC metadata.
Using
exiftool
:Using
piexif
:Using
iptcinfo3
:In Django would look like:
Links:
The text was updated successfully, but these errors were encountered: