问题
How to write CUSTOM metadata into JPEG with Python?
I tried
import piexif
exif_dict = {
'uwi': myvalue1,
'activity_type': myvalue2,
'prediction': myvalue3,
'url_current': myvalue4,
'url_previous': mavalue5
}
exif_bytes = piexif.dump(exif_dict)
with open(filename, "w") as fp:
test_image.save(fp, "JPEG", exif=exif_bytes)
but see nothing in images with XnView
. What am I doing wrong?
P.S. I don't need to write camera model, exposure and other stuff. I want to write my own custom metadata.
回答1:
Check out the docs on how to use piexif. What you are doing wrong for example is trying to write custom metadata and opening the file with open instead of opening with Image from the PIL module.
Cutting down the example from the docs, you could do something like this:
from PIL import Image
import piexif
zeroth_ifd = {
piexif.ImageIFD.Make: u"Canon",
piexif.ImageIFD.XResolution: (96, 1),
piexif.ImageIFD.YResolution: (96, 1),
piexif.ImageIFD.Software: u"piexif"
}
exif_ifd = {
piexif.ExifIFD.DateTimeOriginal: u"2099:09:29 10:10:10",
piexif.ExifIFD.LensMake: u"LensMake",
piexif.ExifIFD.Sharpness: 65535,
piexif.ExifIFD.LensSpecification: ((1, 1), (1, 1), (1, 1), (1, 1)),
}
gps_ifd = {
piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
piexif.GPSIFD.GPSAltitudeRef: 1,
piexif.GPSIFD.GPSDateStamp: u"1999:99:99 99:99:99",
}
first_ifd = {
piexif.ImageIFD.Make: u"Canon",
piexif.ImageIFD.XResolution: (40, 1),
piexif.ImageIFD.YResolution: (40, 1),
piexif.ImageIFD.Software: u"piexif"
}
exif_dict = {"0th":zeroth_ifd, "Exif":exif_ifd, "GPS":gps_ifd, "1st":first_ifd, "thumbnail":thumbnail}
exif_bytes = piexif.dump(exif_dict)
im = Image.open("foo.jpg")
im.save("out.jpg", exif=exif_bytes)
You can check all the metadata fields that you can edit with piexif here.
来源:https://stackoverflow.com/questions/52729428/how-to-write-custom-metadata-into-jpeg-with-python