from PIL import Image
image = Image.open('image.png') #open image
image = image.convert("RGBA") #convert to RGBA
rgb = image.getpixel(x,y) #Get the rgba value at coordinates x,y
rgb[3] = int(rgb[3] / 2) or you could do rgb[3] = 50 maybe? #set alpha to half somehow
image.putpixel((x,y), rgb) #put back the modified reba values at same pixel coordinates
我把Pecan的答案和cr333的问题放在一起:
Using PIL to make all white pixels transparent?
。。。想到这个:
在我的例子中,我有黑色背景的文本,只希望背景是半透明的,在这种情况下:
我意识到这个问题很老了,但是对于当前版本的枕头(v4.2.1),有一个名为
putalpha
的函数。对我来说似乎很管用。我不知道在你需要改变alpha的每一种情况下是否都有效,但它确实有效。它为图像中的每个像素设置alpha值。看起来,虽然你可以使用一个掩码:http://www.leancrew.com/all-this/2013/11/transparency-with-pil/。像这样使用
putalpha
:你能做这样的事吗?
绝对不是最有效的做事方式,但它可能会起作用。我在浏览器中编写了代码,所以它可能不是无错误的,但希望它能给你一个想法。
编辑:刚刚注意到这个问题有多老了。把答案留给将来的帮助。:)
相关问题 更多 >
编程相关推荐