Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何调整我的代码,使我的透明python图像在另一个上面(使用新的着色颜色)呈现为高质量?_Python - Fatal编程技术网

我如何调整我的代码,使我的透明python图像在另一个上面(使用新的着色颜色)呈现为高质量?

我如何调整我的代码,使我的透明python图像在另一个上面(使用新的着色颜色)呈现为高质量?,python,Python,我有两张照片。一个png皮卡丘和一个黑色背景 使用我当前的代码,我成功地将两个图像合并在一起,并对透明的pikachu进行着色,但正如您所看到的,结果并不好。我尝试了多张透明图片,效果相同 以下是我的代码当前的外观: def image_tint(src, tint='#ffffff'): if Image.isStringType(src): # file path? src = Image.open(src) src = src.convert('RGB') if s

我有两张照片。一个png皮卡丘和一个黑色背景

使用我当前的代码,我成功地将两个图像合并在一起,并对透明的pikachu进行着色,但正如您所看到的,结果并不好。我尝试了多张透明图片,效果相同

以下是我的代码当前的外观:

def image_tint(src, tint='#ffffff'):
if Image.isStringType(src):  # file path?
    src = Image.open(src)
    src = src.convert('RGB') 
if src.mode not in ['RGB', 'RGBA']:
    raise TypeError('Unsupported source image mode: {}'.format(src.mode))
src.load()

tr, tg, tb = getrgb(tint)
tl = getcolor(tint, "L")  # tint color's overall luminosity
if not tl: tl = 1  # avoid division by zero
tl = float(tl)  # compute luminosity preserving tint factors
sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb))  # per component
                                                  # adjustments
# create look-up tables to map luminosity to adjusted tint
# (using floating-point math only to compute table)
luts = (tuple(map(lambda lr: int(lr*sr + 0.5), range(256))) +
        tuple(map(lambda lg: int(lg*sg + 0.5), range(256))) +
        tuple(map(lambda lb: int(lb*sb + 0.5), range(256))))
l = grayscale(src)  # 8-bit luminosity version of whole image
if Image.getmodebands(src.mode) < 4:
    merge_args = (src.mode, (l, l, l))  # for RGB verion of grayscale
else:  # include copy of src image's alpha layer
    a = Image.new("L", src.size)
    a.putdata(src.getdata(3))
    merge_args = (src.mode, (l, l, l, a))  # for RGBA verion of grayscale
    luts += tuple(range(256))  # for 1:1 mapping of copied alpha values

return Image.merge(*merge_args).point(luts)

为什么质量如此糟糕?

您的第四行
src=src.convert('RGB')
正在使用
src
并丢弃alpha通道。尝试删除该行,或者改为执行
src=src.convert('RGBA')
,您的结果应该会有所改善。

src=src.convert('RGB')
这条线就是这里的这条线,不是仅仅扔掉了您的alpha通道吗?这条小线是:)通过删除它现在呈现得非常完美。Thx一吨:)让我把它作为一个答案,这样我就可以得到一个甜蜜的答案点D
transparent_files = 'Images/transparentImageFolder' 
static_files = 'Images/staticImageFolder' 

for pathtransparent, dirs, filetransparent in os.walk(transparent_files):
    for pathstatic, dirs, filesstatic in os.walk(static_files): 
        for transparentfile in filetransparent:
            input_image_path = pathtransparent + "/" + transparentfile
            print('tinting "{}"'.format(input_image_path))
            result = image_tint(input_image_path, '#444222') 
            result.save(input_image_path)

            for staticfile in filesstatic:
                staticImage= Image.open(pathstatic + "/" + staticfile, 'r').convert("RGBA")

                transparentImage =  Image.open(pathtransparent + "/" + transparentfile, 'r').convert("RGBA")    
                text_img = Image.new('RGBA', (staticImage.width, staticImage.height), (0, 0, 0, 0))
                text_img.paste(staticImage, ((text_img.width - staticImage.width) // 2, (text_img.height - staticImage.height) // 2))
                text_img.paste(transparentImage, ((text_img.width - transparentImage.width) // 2, (text_img.height - transparentImage.height) 
                text_img.save(pathtransparent + "/" + transparentfile)