在PIL中调整大小的PNG图像的质量

2024-05-13 21:45:06 发布

您现在位置:Python中文网/ 问答频道 /正文

不管我做了什么来降低PNG图像的质量,这都不会改变文件大小,似乎它不适合我,如何使用quality参数来降低图像的质量和文件大小,请参阅下面的代码。在

请注意,原始图像类型是JPEG。在

将低于100的质量更改为0不会起任何作用。在

代码:

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)

Tags: path代码图像truepng质量floatitem