简体   繁体   中英

Pillow - GIF File Size

The following program uses the Pillow package ( 3.4.2 ) to create a very simple GIF file. The file size is 11.2 KB .

from PIL import Image, ImageDraw
img = Image.new('P', (400, 300))
draw = ImageDraw.Draw(img)
draw.rectangle((0, 0, img.width, img.height), fill='black')
draw.line((10, 10, img.width-10, img.height-10), fill='cyan', width=5)
del draw
img.save('Test.gif')

If I open this file in Microsoft Paint and Save As with a different name, the file size becomes 1.90 KB .

Why such a big difference? Can I make Pillow use whatever format the Paint uses to get the same small size?

If your not making a animated GIF you can change img.save('Test.gif') to img.save('Test.png') the file size will then become 1.52KB .

You could use jpeg and optimise parameters as in here

img.save('Test.jpg',optimize=True,quality=95)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM