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

时间:2012-11-15 22:56:02

标签: python image compression

我要求压缩文件大小小于500kb的任何上传图片,我在谷歌上搜索过,我只能看到:

 >>> foo = foo.resize((160,300),Image.ANTIALIAS)
 >>> foo.save("path\\to\\save\\image_scaled.jpg",quality=95)

如果我采用这种方法,我将不得不在压缩后检查图像是否小于500kb,如果没有则再降低质量和尺寸。

有更好的方法吗?

2 个答案:

答案 0 :(得分:11)

预先无法预测JPEG压缩。你描述的方法,压缩&衡量&再试一次,这是我所知道的唯一方式。

您可以尝试使用不同的质量设置压缩大量典型图像,以了解最佳起点,以及猜测设置更改将如何影响大小的方法。这将使您在没有太多迭代的情况下实现最佳尺寸。

您还可以将类似文件的对象传递给无需写入磁盘的save函数,只计算字节数。一旦确定了最佳设置,就可以将其再次保存到实际文件中。

编辑:这是一个合适的字节计数文件对象的实现。只需在保存后检查size

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)

编辑2:这是使用上述内容进行二元搜索,以便在最小尝试次数中获得最佳quality

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

答案 1 :(得分:3)

猜猜我会在这里提供我的代码,以便有人遇到同样的问题

class PhotoField(forms.FileField, object):

    def __init__(self, *args, **kwargs):
        super(PhotoField, self).__init__(*args, **kwargs)
        self.help_text = "Images over 500kb will be resized to keep under 500kb limit, which may result in some loss of quality"

    def validate(self,image):
        if not str(image).split('.')[-1].lower() in ["jpg","jpeg","png","gif"]:
            raise ValidationError("File format not supported, please try again and upload a JPG/PNG/GIF file")

    def to_python(self, image):
        limit = 500000
        img = Image.open(image.file)
        width, height = img.size
        ratio = float(width) / float(height)
        quality = 100
        while len(image.file.read()) > limit:
            width -= 100
            quality -= 10
            height = int(width / ratio)
            img.resize((width, height), Image.ANTIALIAS)
            img.save(image.file.name, "JPEG", quality=quality)
            image.file = open(image.file.name)
            # reset the file pointer to the beginning so the while loop can read properly
            image.file.seek(0)
        return image

http://james.lin.net.nz/2012/11/19/django-snippet-reduce-image-size-during-upload/