使用Pillow和Piexif

时间:2016-07-20 13:45:06

标签: python image exif pillow

我正在创建一个工具来从图像中提取exif数据并删除GPS数据。我用Pillow读取文件。然后我使用以下函数来提取exif并删除GPS数据。

def manipulate_exif(img, remove_gps):

    try:
        import piexif
    except ImportError:
        raise

    try:
        exif = piexif.load(img.info['exif'])

        if remove_gps:
            exif.get('GPS', None).clear()
            print '{}'.format(pprint.pformat(exif))

    except Exception:
        exif = {}

    return piexif.dump(exif)

from PIL import Image
img = Image.open('./IMG_0264.jpg')

# Creates Issue
exif_copy_bad = manipulate_exif(img, True)

# No issue
exif_copy_good = manipulate_exif(img, False)

img.save('./Test_Bad.jpg', 'JPEG', exif=exif_copy_bad)
img.save('./Test_Good.jpg', 'JPEG', exif=exif_copy_good)

当我使用remove_gps标志设置为False时,该功能按预期工作。但是,当我将remove_gps设置为True时,我会在某些字段中出现奇怪的字段溢出问题,并且在之前存在GPS数据的exif文件中出现Bad IFD1 directory错误。

我不确定这是Piexif中的错误还是我错误地使用了Piexif。

0 个答案:

没有答案