python压缩图片,Python图像库(PIL),如何将图像压缩成所需的文件大小?

I got a requirement to compress any uploaded images less than 500kb in file size, I have searched on google and all I can see is:

>>> foo = foo.resize((160,300),Image.ANTIALIAS)

>>> foo.save("path\\to\\save\\image_scaled.jpg",quality=95)

If I go with this approach I will have to check if the image is less than 500kb after compress, if not then go for lower quality and size.

Is there a better way to do it?

解决方案

JPEG compression is not predictable beforehand. The method you described, compress & measure & try again, is the only way I know.

You can try compressing a number of typical images with different quality settings to get an idea of the optimum starting point, plus a way of guessing how changes to the setting will affect the size. That will get you to zero in on the optimum size without too many iterations.

You can also pass a file-like object to the save function that doesn't bother to write to disk, just counts the bytes. Once you've determined the best settings then you can save it again to an actual file.

Edit: Here's an implementation of a suitable byte counting file object. Just check size after the save.

class file_counter(object):

def __init__(self):

self.position = self.size = 0

def seek(self, offset, whence=0):

if whence == 1:

offset += self.position

elif whence == 2:

offset += self.size

self.position = min(offset, self.size)

def tell(self):

return self.position

def write(self, string):

self.position += len(string)

self.size = max(self.size, self.position)

Edit 2: Here's a binary search using the above to get the optimal quality in the smallest number of attempts.

def smaller_than(im, size, guess=70, subsampling=1, low=1, high=100):

while low < high:

counter = file_counter()

im.save(counter, format='JPEG', subsampling=subsampling, quality=guess)

if counter.size < size:

low = guess

else:

high = guess - 1

guess = (low + high + 1) // 2

return low

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值