Python PIL 0.5 不透明度、透明度、alpha
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24731035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python PIL 0.5 opacity, transparency, alpha
提问by Anderson
Is there any way to make an image half transparent?
有没有办法使图像半透明?
the pseudo code is something like this:
伪代码是这样的:
from PIL import Image
image = Image.open('image.png')
image = alpha(image, 0.5)
I googled it for a couple of hours but I can't find anything useful.
我用谷歌搜索了几个小时,但找不到任何有用的东西。
回答by Pecans
Could you do something like this?
你能做这样的事情吗?
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
Definitely not the most efficient way of doing things but it might work. I wrote the code in browser so it might not be error free but hopefully it can give you an idea.
绝对不是最有效的做事方式,但它可能会奏效。我在浏览器中编写了代码,所以它可能不会出错,但希望它能给你一个想法。
EDIT: Just noticed how old this question was. Leaving answer anyways for future help. :)
编辑:刚刚注意到这个问题有多老了。无论如何留下答案以备将来帮助。:)
回答by Vlad
I put together Pecan's answer and cr333's question from this question:
我从这个问题中将 Pecan 的回答和 cr333 的问题放在一起:
Using PIL to make all white pixels transparent?
... and came up with this:
...并想出了这个:
from PIL import Image
opacity_level = 170 # Opaque is 255, input between 0-255
img = Image.open('img1.png')
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
newData.append((0, 0, 0, opacity_level))
else:
newData.append(item)
img.putdata(newData)
img.save("img2.png", "PNG")
In my case, I have text with black background and wanted only the background semi-transparent, in which case:
就我而言,我有黑色背景的文本,只想要背景半透明,在这种情况下:
from PIL import Image
opacity_level = 170 # Opaque is 255, input between 0-255
img = Image.open('img1.png')
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[0] == 0 and item[1] == 0 and item[2] == 0:
newData.append((0, 0, 0, opacity_level))
else:
newData.append(item)
img.putdata(newData)
img.save("img2.png", "PNG")
回答by NaomiW
I just did this by myself...even though my code maybe a little bit weird...But it works fine. So I share it here. Hopes it could help anybody. =)
我只是自己做的...即使我的代码可能有点奇怪...但它工作正常。所以我在这里分享。希望它可以帮助任何人。=)
The idea: To transparent a pic means lower alpha which is the 4th element in the tuple.
想法:透明图片意味着较低的alpha,它是元组中的第4个元素。
my frame code:
我的框架代码:
from PIL import Image
img=open(image)
img=img.convert('RGBA') #you can make sure your pic is in the right mode by check img.mode
data=img.getdata() #you'll get a list of tuples
newData=[]
for a in data:
a=a[:3] #you'll get your tuple shorten to RGB
a=a+(100,) #change the 100 to any transparency number you like between (0,255)
newData.append(a)
img.putdata(newData) #you'll get your new img ready
img.save(filename.filetype)
I didn't find the right command to fulfil this job automatically, so I write this by myself. Hopes it'll help again. XD
我没有找到自动完成这项工作的正确命令,所以我自己写了这个。希望它会再次帮助。XD
回答by Clayton Geist
I realize this question is really old, but with the current version of Pillow (v4.2.1), there is a function called putalpha
. It seems to work fine for me. I don't know if will work for every situation where you need to change the alpha, but it does work. It sets the alpha value for every pixel in the image. It seems, though that you can use a mask: http://www.leancrew.com/all-this/2013/11/transparency-with-pil/.
我意识到这个问题真的很老,但是在 Pillow (v4.2.1) 的当前版本中,有一个名为putalpha
. 这对我来说似乎很好。我不知道是否适用于您需要更改 alpha 的每种情况,但它确实有效。它为图像中的每个像素设置 alpha 值。看来,虽然你可以使用面具:http: //www.leancrew.com/all-this/2013/11/transparency-with-pil/。
Use putalpha
like this:
putalpha
像这样使用:
from PIL import Image
img = Image.open(image)
img.putalpha(128) # Half alpha; alpha argument must be an int
img.save(dest)