I want to add a copyright notice into exif data of my displayed images.
I'm using django-imagekit
and piexif
libraries.
I can't manage to preserve nor enrich metadata of an image when I upload then process them in my Django admin.
Here is my model :
class Still(models.Model):
image = ProcessedImageField(upload_to='images',
processors=[ResizeToFit(1024)],
format='JPEG',
options={'subsampling': 0, 'optimize': True, 'quality': 85}
)
img_th_watermarked = ImageSpecField(source='image',
processors=[ResizeToFit(width=612), TextOverlayProcessor(), ExifCopyrightProcessor()],
)
Here is the processor :
def add_copyright_exif(image):
copyright = "© {} my name".format(datetime.now().year)
try:
exif_dict = piexif.load(image.info["exif"])
except KeyError as k:
exif_dict = {"0th":{}}
exif_dict["0th"][piexif.ImageIFD.Copyright] = copyright
exif_bytes = piexif.dump(exif_dict)
new_image = image.copy()
new_image.info["exif"] = exif_bytes
print(new_image.info)
return new_image
class ExifCopyrightProcessor(object):
def process(self, image):
return add_copyright_exif(image)
The print
seems to display the correct info, other processors work fine, but metadata get lost on the way. I tried removing format option as well. I don't mind that metadata get lost at the ProcessedImageField
step, but I should be able to add new info at the ImageSpecField
step.
Images and cached versions are stored on S3.
Thanks in advance for your help.