?Exif详细描述参考:
Exif文件格式描述_石旭东的技术博客_51CTO博客
# from PIL import Image
# import piexif
#
# im = Image.open("D://temp//1.jpg")
# exif_dict = piexif.load(im.info["exif"])
#
# print(type(exif_dict), exif_dict)
#
# for ifd in ("0th", "Exif", "GPS", "1st"):
# for tag in exif_dict[ifd]:
# print(piexif.TAGS[ifd][tag], exif_dict[ifd][tag])
# exif_dict["0th"][piexif.ImageIFD.Artist] = "中国科技".encode()
# exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = '9999:09:09 09:09:09'.encode()
# # print(exif_dict["GPS"][piexif.ImageIFD])
# exif_bytes = piexif.dump(exif_dict)
# print(type(exif_dict), exif_dict)
# im.save("D://temp//hint2.jpg", exif=exif_bytes)
# print('------------------------------修改后-----------------------------------')
# for ifd in ("0th", "Exif", "GPS", "1st"):
# for tag in exif_dict[ifd]:
# print(piexif.TAGS[ifd][tag], exif_dict[ifd][tag])
import io
import piexif
from PIL import Image
# 读取图片并创建缩略图
bytesIO = io.BytesIO()
thumb_im = Image.open('D://temp//1.jpg')
thumb_im.thumbnail((50, 50), Image.ANTIALIAS)
thumb_im.save(bytesIO, 'jpeg')
thumbnail = bytesIO.getvalue()
# 编辑EXIF数据
zeroth_ifd = {
piexif.ImageIFD.Make: '中国'.encode(),
piexif.ImageIFD.XResolution: (300, 1),
piexif.ImageIFD.YResolution: (300, 1),
piexif.ImageIFD.Software: '中国'.encode(),
piexif.ImageIFD.Artist : "中国科技".encode(),
piexif.ImageIFD.Copyright: "中国科技".encode()
}
exif_ifd = {
piexif.ExifIFD.DateTimeOriginal: '2099:09:29 10:10:10',
piexif.ExifIFD.LensMake: "中国科技".encode(),
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: '1999:99:99 99:99:99',
}
first_ifd = {
piexif.ImageIFD.Make: "中国科技".encode(),
piexif.ImageIFD.XResolution: (40, 1),
piexif.ImageIFD.YResolution: (40, 1),
piexif.ImageIFD.Software: "中国科技".encode()
}
# 保存
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('D://temp//1.jpg')
im.save('D://temp//hint2.jpg', exif=exif_bytes)
# 读取EXIF
exif_dict = piexif.load('D://temp//hint2.jpg')
for k, v in exif_dict.items():
print(k, v)
# 0th {271: b'Canon', 282: (96, 1), 283: (96, 1), 305: b'piexif', 34665: 115, 34853: 226}
# Exif {36867: b'2099:09:29 10:10:10', 41994: 65535, 42034: ((1, 1), (1, 1), (1, 1), (1, 1)), 42035: b'LensMake'}
# GPS {0: (2, 0, 0, 0), 5: 1, 29: b'1999:99:99 99:99:99'}
# Interop {}
# 1st {271: b'Canon', 282: (40, 1), 283: (40, 1), 305: b'piexif', 513: 391, 514: 1556}
# thumbnail b'……'
|