为了减小要在网站中使用的图像的大小,我将质量降低到 80-85%。这会在一定程度上减小图像尺寸。
为了在不影响质量的情况下进一步减小尺寸,我的 friend 指出来自相机的原始图像有很多称为 Exif 信息的元数据。由于不需要为网站中的图像保留此 Exif 信息,因此我们可以将其删除。这将进一步减少 3-10 kB 的大小。
但我无法在我的 Python 代码中找到合适的库来执行此操作。我浏览了相关问题并尝试了一些方法:
原图:http://mdb.ibcdn.com/8snmhp4sjd75vdr27gbadolc003i.jpg
-
/usr/local/bin/mogrify -strip filename
结果:http://s23.postimg.org/aeaw5x7ez/8snmhp4sjd75vdr27gbadolc003i_mogrify.jpg 这种方法将大小从 105 kB 减小到 99.6 kB,但也改变了颜色质量。
-
exiftool -all= filename
结果:http://s22.postimg.org/aiq99o775/8snmhp4sjd75vdr27gbadolc003i_exiftool.jpg 这种方法将大小从 105 kB 减小到 72.7 kB,但也改变了颜色质量。
- 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/