简体   繁体   中英

PNG Image Quality in PIL

Have been trying to upload the following png file to google cloud storage through app engine:

在此处输入图片说明

Before the upload, I'm running this through PIL to take care of any image rotations or changes in background color etc

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

在此处输入图片说明

Anybody have ideas?

For the PIL commands, I'm just running the following:

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:

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.

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