如何使用Python将CUSTOM元数据写入JPEG? [英] How to write CUSTOM metadata into JPEG with Python?

查看:154
本文介绍了如何使用Python将CUSTOM元数据写入JPEG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python将CUSTOM元数据写入JPEG?

How to write CUSTOM metadata into JPEG with Python?

我尝试过

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)

,但是在带有XnView的图像中什么也看不到.我在做什么错了?

but see nothing in images with XnView. What am I doing wrong?

P.S.我不需要编写相机模型,曝光和其他内容.我想编写自己的自定义元数据.

P.S. I don't need to write camera model, exposure and other stuff. I want to write my own custom metadata.

推荐答案

查看关于如何使用piexif的文档.例如,您做错的是尝试编写自定义元数据并使用open打开文件,而不是使用PIL模块中的Image打开.

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)

您可以检查所有可以使用piexif编辑的元数据字段,此处.

You can check all the metadata fields that you can edit with piexif here.

这篇关于如何使用Python将CUSTOM元数据写入JPEG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆