2016-03-15 70 views
0

好的,首先要做的第一件事。这是this question的近似重复。使用Python Imaging Library在图像顶部覆盖彩色像素

但是,我面临的问题在关键方面略有不同。

在我的应用程序中,我读取了一个通用文件名,加载所述图像并显示它。在那里变得棘手的是我覆盖了“突出显示”的外观。为此,我使用了Image.blend()函数,并将其与直黄色图像混合。

但是,当处理混合时,我打错了两个图像不兼容混合的错误。为了解决这个问题,我打开了绘画中的样本图像,并在整件事上涂上黄色,并将其保存为副本。

刚才我发现,当通过文件名读入不同类型的图像时,这会失败。请记住,这需要是通用的。

所以我的问题是:而不是手动复制的图像,我可以通过复制图像和修改它,以便它是纯黄色生成一个python?注意:我不需要在保存之后保存它,所以实现它就足够了。

不幸的是,我不能分享我的代码,但希望下面就给什么,我需要一个想法:

from PIL import Image 

desiredWidth = 800 
desiredHeight = 600 

primaryImage = Image.open("first.jpg").resize((desiredWidth, desiredHeight), Image.ANTIALIAS) 

# This is the thing I need fixed: 
highlightImage = Image.open("highlight.jpg").resize((desiredWidth, desiredHeight), Image.ANTIALIAS) 

toDisplay = Image.blend(primaryImage, highlightImage, 0.3) 

由于任何人谁可以提供帮助。

+0

'黄色=(255,255,0); Image.new(primaryImage.mode,primaryImage.size,黄色)'? –

回答

0

听起来像是你想使一个new图像:

fill_color = (255,255,0) #define the colour as (R,G,B) tuple 

highlightImage = Image.new(primaryImage.mode, #same mode as the primary 
          primaryImage.size, #same size as the primary 
          fill_color)#and the colour defined above 

此创建了相同模式和大小已经打开的图像new形象,而是用纯色。干杯。

此外,如果你正在使用的不是原装PIL枕头,你甚至可以通过名字来取得的颜色:

from PIL.ImageColor import getcolor 

overlay = 'yellow' 
fill_color = getcolor(overlay, primaryImage.mode) 
+1

我亲爱的先生(或女士),你很美。这工作完美无瑕。非常感谢你。 P.S.可悲的是,投票是隐形的,但它在那里。 – kirypto