1

我正在尝试将三个图像组合在一起。我想要在底部的图像是全黑像素的 700x900 图像。最重要的是,我想粘贴一个 400x400 的图像,偏移量为 100,200。最重要的是,我想粘贴一个 700x900 的图像边框。图像边框内部有 alpha=0,周围有 alpha=0,因为它没有直边。当我运行下面粘贴的代码时,我遇到了 2 个问题:

1) 在 alpha 通道 = 0 的边框图像上,alpha 通道已设置为 255,并且显示白色而不是黑色背景和我放置边框的图像。

2) 边框图像的质量已显着降低,看起来与应有的不同。

另外:边框图像的一部分将覆盖我放置边框的图像的一部分。所以我不能只是切换我粘贴的顺序。

提前感谢您的帮助。

#!/usr/bin/python -tt

from PIL import ImageTk, Image

old_im2 = Image.open('backgroundImage1.jpg') # size = 400x400
old_im = Image.open('topImage.png') # size = 700x900
new_size = (700,900)
new_im = Image.new("RGBA", new_size) # makes the black image
new_im.paste(old_im2, (100, 200))
new_im.paste(old_im,(0,0))

new_im.show()
new_im.save('final.jpg')
4

1 回答 1

2

我认为您对图像有误解 - 边框图像确实到处都有像素。它不可能是“丢失”的像素。可以有一个带有 alpha 通道的图像,它是一个类似于 、 和 通道的R通道GB但表示透明度。

试试这个:

1. 确保topImage.png有一个透明通道,并且您想要“丢失”的像素是透明的(即具有最大 alpha 值)。您可以通过以下方式仔细检查:

print old_im.mode  # This should print "RGBA" if it has an alpha channel.

2.new_im在“RGBA”模式下创建:

new_im = Image.new("RGBA", new_size) # makes the black image
# Note the "A" --------^

3. 试试这个粘贴语句:

new_im.paste(old_im,(0,0), mask=old_im)  # Using old_im as the mask argument should tell the paste function to use old_im's alpha channel to combine the two images.
于 2013-08-20T20:25:31.097 回答