简体   繁体   English

PIL 中的 PNG 图像质量

[英]PNG Image Quality in PIL

Have been trying to upload the following png file to google cloud storage through app engine:一直在尝试通过应用引擎将以下 png 文件上传到谷歌云存储:

在此处输入图片说明

Before the upload, I'm running this through PIL to take care of any image rotations or changes in background color etc在上传之前,我通过 PIL 运行它来处理任何图像旋转或背景颜色的变化等

However, I'm getting really bad image quality when running PIL manipulations through the app even though running the same commands in python command line comes out fine但是,通过应用程序运行 PIL 操作时,我的图像质量非常差,即使在 python 命令行中运行相同的命令效果很好

在此处输入图片说明

Anybody have ideas?有人有想法吗?

For the PIL commands, I'm just running the following:对于 PIL 命令,我只是运行以下命令:

imtemp = Image.open('/[path]/logo.png')
size = max(imtemp.size[0],imtemp.size[1]) 
im = Image.new('RGBA', (size,size), (255,255,255,0))
im.paste(imtemp, ((size-imtemp.size[0])/2,(size-imtemp.size[1])/2)) 
imtemp = im 
im = Image.new('RGB', (size,size), '#FFFFFF') 
im.paste(imtemp, (0,0), imtemp) 
im.show()

Have tried below, but still no luck已尝试以下,但仍然没有运气

    imtemp = Image.open(StringIO(imagedata)).convert("RGBA")
    im = Image.new("RGB", imtemp.size, "#FFFFFF")
    im.paste(imtemp, None, imtemp)
    imageoutput = StringIO()
    im.save(imageoutput, format="PNG", quality=85, optimize=True, progressive=True)
    imageoutput = imageoutput.getvalue()

It looks like you want to take a palettized image, possibly with transparent pixels, project it on a white background and make a quality-resized version of it which is half as big.看起来您想要拍摄一个调色板图像,可能带有透明像素,将其投影到白色背景上,然后制作一个质量调整大小的一半大小的版本。

You can use the convert() and thumbnail() function for this:您可以为此使用convert()thumbnail()函数:

from PIL import Image

# Open the image and convert it to RGBA.
orig = Image.open("fresh.png").convert("RGBA")

# Paste it onto a white background.
im = Image.new("RGB", orig.size, "#ffffff")
im.paste(orig, None, orig)

# Now a quality downsize.
w, h = im.size
im.thumbnail((w / 2, h / 2), Image.ANTIALIAS)
im.show()    

Of course, you can leave the thumbnail() call out if you want the image at the original size.当然,如果您想要原始尺寸的图像,您可以保留thumbnail()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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