用PIL删除exif时文件大小是增加而不是减少?

时间:2019-11-12 15:34:28

标签: python python-2.7 python-imaging-library filesize

没什么大不了的,但是为了了解发生了什么事情以避免将来的问题...

我想知道为什么在删除exif数据后jpg的文件大小会增加。我以为应该下降吗?

from PIL import Image

image = Image.open(fname)

name, ext = get_fname_ext(fname)

out_name = name + '_cleaned' + ext
out_abs = output_dir + "\\" + out_name

image.save(out_abs)

之前的文件大小:192.65 KB

之后的文件大小:202.46 KB

差异:+9.82 KB

1 个答案:

答案 0 :(得分:1)

这里发生的是PIL重新压缩图像(源是JPG,但不是必须的,因此将其视为图像数据)。依靠外部工具,例如exiftool,imagemagick或jpegtran会更安全/容易。 this related SO question上的答案可能是很好的资源。

作为仅PIL的替代方法,您可以尝试使用this answer中的python代码段为您工作:

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')
相关问题