1

As part of an app using Pillow, I want to update exif for a bunch of photos, including artist names, titles, etc. So far, I've been using piexif to do so. However I ran into an encoding problem:

import PIL.Image
import piexif

img = PIL.Image.open("photo.jpg")
exif_dict = piexif.load(img.info["exif"])
exif_dict.get("0th").update({
        315: "Lebœuf",  # ExifTag for "Artist"
    })
img.save("photo.jpg", "jpeg", exif=exif_dict)

UnicodeEncodeError: 'latin-1' codec can't encode character '\u0153' in position 3: ordinal not in range(256)

Encoding and its errors are a nightmare, one life will not suffice for me to grasp only the spirit of it. I understand that the character œ is not part of the latin1 encoding, which piexif uses without control over it. However, if I add the exif tag manually on, say, Windows 10 explorer, I can successfully add that character and even retrieve it through piexif.load(). My understanding is such as it seems not necessary to impose the latin1 encoding? Is there a workaround or another module I could use?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • In a classic fashion, after a couple days struggling it's only after posting the question that I find the workaround I was looking for: ```py exif_dict.get("0th").update({ 315: "Lebœuf".encode(), }) ``` This does work. The error is not thrown and the exif shows the correct character. I don't post this as an answer for the reason that I am still unsure why this works, being so little knowledgable in the matters of encoding. Hope it helps my neighbour, however. – Paulo Modulo Nov 09 '21 at 15:57
  • Related: [Python's exif module and Umlauts in JPEG Metadata](https://stackoverflow.com/questions/65720067/) – JosefZ Nov 09 '21 at 17:01
  • You can add the solution as an actual answer (and even mark it as accepted). – mkrieger1 Feb 12 '22 at 11:46

0 Answers0