本文介绍了使用PIL缩放图像以保持透明度和色彩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要缩放透明图像,但尚不知道稍后将其合成到的背景颜色.不幸的是,PIL似乎并入了完全透明像素的颜色值,从而导致不良结果.有没有办法告诉PIL调整大小忽略完全透明的像素?

Say you want to scale a transparent image but do not yet know the color(s) of the background you will composite it onto later. Unfortunately PIL seems to incorporate the color values of fully transparent pixels leading to bad results. Is there a way to tell PIL-resize to ignore fully transparent pixels?

import PIL.Image

filename = "trans.png"  # http://qrc-designer.com/stuff/trans.png
size = (25,25)

im = PIL.Image.open(filename)
print im.mode  # RGBA

im = im.resize(size, PIL.Image.LINEAR)  # the same with CUBIC, ANTIALIAS, transform
# im.show()  # does not use alpha
im.save("resizelinear_"+filename)


# PIL scaled image has dark border

具有(0,0,0,0)(黑色但完全透明)背景(左)的原始图像

original image with (0,0,0,0) (black but fully transparent) background (left)

输出带有黑色光晕的图像(中间)

output image with black halo (middle)

使用gimp缩放的正确输出(右)

proper output scaled with gimp (right)

edit:看起来要实现我想要的功能,就必须修改resize函数本身的采样,以使其忽略具有完全透明性的像素.

edit: It looks like to achieve what I am looking for I would have to modify the sampling of the resize function itself such that it would ignore pixels with full transparency.

edit2:我发现了一个非常丑陋的解决方案.它将完全透明像素的颜色值设置为周围非完全透明像素的平均值,以在调整大小时最大程度地减小完全透明像素颜色的影响.简单形式的速度很慢,但是如果没有其他解决方案,我会发布.通过使用膨胀操作仅处理必要的像素,可以使其更快.

edit2: I have found a very ugly solution. It sets the color values of fully transparent pixels to the average of the surrounding non fully transparent pixels to minimize impact of fully transparent pixel colors while resizing. It is slow in the simple form but I will post it if there is no other solution. Might be possible to make it faster by using a dilate operation to only process the necessary pixels.

edit3:预乘alpha是必经之路-参见Mark的答案

edit3: premultiplied alpha is the way to go - see Mark's answer

推荐答案

似乎PIL在调整大小之前不进行alpha预乘,这对于获得正确的结果是必要的.幸运的是,用蛮力很容易做到.然后,您必须对调整大小后的结果进行相反的操作.

It appears that PIL doesn't do alpha pre-multiplication before resizing, which is necessary to get the proper results. Fortunately it's easy to do by brute force. You must then do the reverse to the resized result.

def premultiply(im):
    pixels = im.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            r, g, b, a = pixels[x, y]
            if a != 255:
                r = r * a // 255
                g = g * a // 255
                b = b * a // 255
                pixels[x, y] = (r, g, b, a)

def unmultiply(im):
    pixels = im.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            r, g, b, a = pixels[x, y]
            if a != 255 and a != 0:
                r = 255 if r >= a else 255 * r // a
                g = 255 if g >= a else 255 * g // a
                b = 255 if b >= a else 255 * b // a
                pixels[x, y] = (r, g, b, a)

结果:

这篇关于使用PIL缩放图像以保持透明度和色彩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:16