how to write exif tags with Python in a self generated image

457 views Asked by At

I have generated an image with pillow and now I want to add metadata to the image. In my image it didn't have a data structure yet, I suppose I have to create one first but how do I do that?

Reading or changing tags from existing images with exif data structure is very easy with the "exif" tool.

Every hint is welcome.

Unfortunately, I am somewhat at a loss and have not yet found any useful information on the Internet.

1

There are 1 answers

0
PyNub On
from PIL import Image, PngImagePlugin

#path to image file
file = "myimage.png"

#create the metadata object
pngMetaData = PngImagePlugin.PngInfo()

#add the data
pngMetaData.add_text('key 1', 'value 1')
pngMetaData.add_text('key 2', 'value 2')

#open the image file and save the metadata
with Image.open(file) as image_file:
    image_file.save(file, pnginfo=pngMetaData)