代码之家  ›  专栏  ›  技术社区  ›  Ossama

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

  •  0
  • Ossama  · 技术社区  · 7 年前

    无论我如何降低PNG图像的质量,这都不会改变文件大小,似乎它对我不起作用,如何使用质量参数来降低图像的质量,从而降低图像的文件大小,请参阅下面的代码。

    请注意,原始图像类型为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)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Ossama    7 年前

    我最终使用了 压缩\u级别

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

    也可以使用以下选项: 优化=真