Python修改图片Exif属性
安装piexif包
pip install piexif
获取图片Exif 信息
exif_dict = piexif.load("foo1.jpg") for ifd in ("0th", "Exif", "GPS", "1st"): for tag in exif_dict[ifd]: print(piexif.TAGS[ifd][tag]["name"], exif_dict[ifd][tag])
import piexif from io import BytesIO def add_exif_data(photo_path=None, photo_data=None): """ 修改图片exif信息 https://www.coder.work/article/6689828 """ exif_stream = BytesIO() if photo_data: exif_dict = piexif.load(photo_data) photo = photo_data elif photo_path: exif_dict = piexif.load(photo_path) photo = photo_path else: return b"" exif_dict["0th"][piexif.ImageIFD.Artist] = "一天一点".encode("utf-8") exif_dict["0th"][piexif.ImageIFD.Software] = "作图吧APP".encode("utf-8") exif_bytes = piexif.dump(exif_dict) piexif.insert(exif_bytes, photo, exif_stream) return exif_stream.getvalue() if __name__ == "__main__": r_stream = add_exif_data(photo_path="/tmp/001.jpg", photo_data=None) with open("/tmp/web/1111.jpg", "wb") as f: f.write(r_stream)