简体   繁体   中英

Adjust the quality of a resized PNG image in PIL

No matter what I do to reduce the quality of the PNG image, this does not chnage the file size, it appears that it is not working with me, how can I use quality argument to reduce the quality and hence the file size of the image, see my code below.

Note that the original image type is JPEG.

Changing the quality below from 100 to 0 does not do anything.

Code:

basewidth = 400
im = Image.open(path+item)
try:
    for orientation in ExifTags.TAGS.keys():
        if ExifTags.TAGS[orientation]=='Orientation':
            break
    exif=dict(im._getexif().items())

    if exif[orientation] == 3:
        im=im.rotate(180, expand=True)
    elif exif[orientation] == 6:
        im=im.rotate(270, expand=True)
    elif exif[orientation] == 8:
        im=im.rotate(90, expand=True)
    im.save(path+item)

except:
    print "exception"
    pass
im = add_corners(im, 180)
f, e = os.path.splitext(path+item)
wpercent = (basewidth/float(im.size[0])) 
hsize = int((float(im.size[1])*float(wpercent))) 
imResize = im.resize((basewidth,hsize), Image.ANTIALIAS)
imResize.save(f + '.png', format="PNG", quality=10, optimize=True)

I ended up using compress_level

imResize.save(f + '.png', format="PNG", compress_level=5)

also the following can be used optimize=True

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