Python:从图像中删除 Exif 信息

标签 python image-processing python-imaging-library exif exiftool

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

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

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

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

  1. Mogrify

    /usr/local/bin/mogrify -strip filename
    

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

  2. Exif-tool

    exiftool -all= filename
    

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

  3. This answer详细解释了如何操作 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')

关于Python:从图像中删除 Exif 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19786301/

相关文章:

python - 如何在 virtualenv 中修改 pip?

python - 我们如何使用均值滤波器作为自适应滤波器?

python - 显示灰度图像

python - 在 PIL 中旋转一个正方形

python - 使用 OpenCV Python 调整图像大小的最佳方法

python - 有没有办法控制 Apache Arrow 批量大小?

python - 如果用户输入字符串而不是整数,如何显示消息?

python - 为什么我会收到 Keras LSTM RNN input_shape 错误?

java - Android图像处理

algorithm - 平面到圆柱体的投影