如何减少PIL中的png图像文件大小 [英] how to reduce png image filesize in PIL
问题描述
我已经使用PIL将jpg/bmt转换和调整为png.我可以轻松地将jpg/bmt转换为png,但是新图像的文件大小太大
I have used PIL to convert and resize jpg/bmt to png .. i can easily resize and convert to png but the file size is of new image is too big
im=Image.open(p1.photo)
im_resize = im.resize((400, 400), Image.ANTIALIAS) # best down-sizing filter
im.save(str(merchant.id)+'_logo.'+'png')
我还需要做些什么来减小图像尺寸?
what else i have to do to reduce image size?
推荐答案
PNG图像仍然必须保留图像上每个像素的所有数据,因此压缩距离有限制.
PNG Images still have to hold all data for every single pixel on the image, so there is a limit on how far you can compress them.
进一步减少它的一种方法是使用索引模式,因为您的400x400会被用作缩略图",
One way to further decrease it, since your 400x400 is to be used as a "thumbnail" of sorts, is to use indexed mode:
im_indexed = im_resize.convert("P") im_resize.save(...)
im_indexed = im_resize.convert("P") im_resize.save(... )
* 等待* 刚刚在您的示例代码中看到一个错误: 您要保存的是原始图像,而不是调整大小的图像:
*wait * Just saw an error in your example code: You are saving the original image, not the resized image:
im=Image.open(p1.photo)
im_resize = im.resize((400, 400), Image.ANTIALIAS) # best down-sizing filter
im.save(str(merchant.id)+'_logo.'+'png')
何时应该做:
im_resize.save(str(merchant.id)+'_logo.'+'png')
您只是保存了原始图像,这就是为什么它看起来很大的原因.也许您不需要使用索引模式.
You are just saving back the original image, that is why it looks so big. Probably you won't need to use indexed mode them.
另一件事:索引模式图像看起来可能很差-如果需要,一种更好的解决方法可能是将您的小尺寸保存为.jpg而不是.png-这些可以根据需要变小,以质量换取价格.
Aother thing: Indexed mode images can look pretty poor - a better way out, if you come to need it, might be to have your smalle sizes saved as .jpg instead of .png s - these can get smaller as you need, trading size for quality.
这篇关于如何减少PIL中的png图像文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!