PIL: Image resizing : Algorithm similar to firefox's

前端 未结 4 1986
刺人心
刺人心 2021-02-03 11:35

I\'m getting about the same bad looking resizing from all the 4 algorithms of PIL

>>> data = utils.fetch(\"http://wavestock.com/images/beta-ico         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-03 11:58

    The image posted is an indexed image. Its mode is 'P' if you read it via PIL. The PIL documentation on Image.resize() for the resample parameter states that:

    If the image has mode “1” or “P”, it is always set to PIL.Image.NEAREST

    So PIL will use nearest filter for resizing indexed images, which is a low quality filter.

    The right way is to convert the image mode to RGB and then use resize with a high-quality filter such as Image.LANCZOS. By the way, Image.ANTIALIAS is now the same as Image.LANCZOS, source here.

    import Image
    img = Image.open("beta-icon.gif").convert("RGB")
    img = img.resize((36,36), Image.LANCZOS)
    img.save("img-resized.png")
    

提交回复
热议问题