PIL 中的 PNG 图像质量
PNG Image Quality in PIL
一直在尝试通过 App Engine 将以下 png 文件上传到 google 云存储:
在上传之前,我运行通过 PIL 处理任何图像旋转或背景颜色变化等
但是,当 运行 通过应用程序进行 PIL 操作时,即使 运行 python 命令行中的相同命令运行正常,我的图像质量也非常差
有人有想法吗?
对于 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()
下面已经试过了,还是不行
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()
您似乎想要拍摄一张可能带有透明像素的调色板图像,将其投影到白色背景上并制作其质量调整后大小的一半大小的版本。
您可以为此使用 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()
当然,如果您想要原始尺寸的图像,您可以保留 thumbnail()
标注。
一直在尝试通过 App Engine 将以下 png 文件上传到 google 云存储:
在上传之前,我运行通过 PIL 处理任何图像旋转或背景颜色变化等
但是,当 运行 通过应用程序进行 PIL 操作时,即使 运行 python 命令行中的相同命令运行正常,我的图像质量也非常差
有人有想法吗?
对于 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()
下面已经试过了,还是不行
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()
您似乎想要拍摄一张可能带有透明像素的调色板图像,将其投影到白色背景上并制作其质量调整后大小的一半大小的版本。
您可以为此使用 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()
当然,如果您想要原始尺寸的图像,您可以保留 thumbnail()
标注。