How to reduce the image file size using PIL

后端 未结 4 1368
抹茶落季
抹茶落季 2020-11-28 03:11

I am using PIL to resize the images there by converting larger images to smaller ones. Are there any standard ways to reduce the file size of the image without losing the qu

4条回答
  •  攒了一身酷
    2020-11-28 03:55

    lets say you have a model called Book and on it a field called 'cover_pic', in that case, you can do the following to compress the image:

    from PIL import Image
    b = Book.objects.get(title='Into the wild')
    image = Image.open(b.cover_pic.path)
    image.save(b.image.path,quality=20,optimize=True)
    

    hope it helps to anyone stumbling upon it.

提交回复
热议问题