您当前的位置:首页 > 计算机 > 编程开发 > Python

Python修改图片Exif属性

时间:08-17来源:作者:点击数:
CDSY,CDSY.XYZ

安装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.cdsy.xyz
    """
    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)
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐