Python:从图像中删除 Exif 信息

2024-03-10

为了减小网站中使用的图像大小,我将质量降低到 80-85%。这在一定程度上大大减小了图像尺寸。

为了在不影响质量的情况下进一步减小尺寸,我的朋友指出,来自相机的原始图像有很多称为 Exif 信息的元数据。由于网站中的图像不需要保留此 Exif 信息,因此我们可以将其删除。这将进一步减少 3-10 kB 的大小。

但我无法找到合适的库来在我的 Python 代码中执行此操作。我浏览了相关问题并尝试了一些方法:

原图:http://mdb.ibcdn.com/8snmhp4sjd75vdr27gbadolc003i.jpg http://mdb.ibcdn.com/8snmhp4sjd75vdr27gbadolc003i.jpg

  1. Mogrify http://www.imagemagick.org/www/mogrify.html

    /usr/local/bin/mogrify -strip filename
    

    Result: http://s23.postimg.org/aeaw5x7ez/8snmhp4sjd75vdr27gbadolc003i_mogrify.jpg http://s23.postimg.org/aeaw5x7ez/8snmhp4sjd75vdr27gbadolc003i_mogrify.jpg此方法将大小从 105 kB 减小到 99.6 kB,但也改变了颜色质量。

  2. Exif工具 https://exiftool.org/

    exiftool -all= filename
    

    Result: http://s22.postimg.org/aiq99o775/8snmhp4sjd75vdr27gbadolc003i_exiftool.jpg http://s22.postimg.org/aiq99o775/8snmhp4sjd75vdr27gbadolc003i_exiftool.jpg此方法将大小从 105 kB 减小到 72.7 kB,但也改变了颜色质量。

  3. 这个答案 https://stackoverflow.com/questions/765396/exif-manipulation-library-for-python/765403#765403详细解释了如何操作 Exif 信息,但如何使用它来删除信息呢?

谁能帮我删除所有额外的元数据而不更改图像的颜色、尺寸和其他属性?


from PIL import Image

image = Image.open('image_file.jpeg')
    
# next 3 lines strip exif
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)
    
image_without_exif.save('image_file_without_exif.jpeg')

# as a good practice, close the file handler after saving the image.
image_without_exif.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python:从图像中删除 Exif 信息 的相关文章

随机推荐