英文:
How to solve problem with transparent area in PNG image?
问题
I use Paint.Net in Windows to make mask png image from source png.
def mask(im):
newimdata = []
transparent = (255, 255, 255, 0)
black = (0, 0, 0)
white = (255, 255, 255)
for color in im.getdata():
if color == transparent:
newimdata.append(white)
else:
newimdata.append(black)
newim = Image.new(im.mode, im.size)
newim.putdata(newimdata)
return newim
img = Image.open(thumb)
img = img.convert("RGBA")
mask(img).show()
The result is a little weird.
Source png:
Mask png:
Left transparent rectangle I made in PaintNet: I clicked the mouse, made a transparent area.
Right transparent rectangle I made: I clicked the mouse, made a transparent area. After I clicked the mouse once again and made transparent vertical figures on the transparent rectangle.
I don't understand: Is it two transparent layers (right rectangle and vertical figures)?
How can I merge this to make the mask as in the clean left rectangle?
英文:
I use Paint.Net in Windows to make mask png image from source png.
def mask (im):
newimdata = []
transparent = (255, 255, 255, 0)
black = (0,0,0)
white = (255,255,255)
for color in im.getdata():
if color == transparent:
newimdata.append(white)
else:
newimdata.append(black)
newim = Image.new(im.mode,im.size)
newim.putdata(newimdata)
return newim
img = Image.open(thumb)
img = img.convert("RGBA")
mask(img).show()
The result is little weird.
Source png.
Mask png.
Left transparent rectangle I made in PaintNet: I clicked mouse, made transparent area.
Right transparent rectangle I made: I clicked mouse, made transparent area. After I clicked mouse once again and made transparent vertical figures on transparent rectangle.
I don't understand: Is it two transparent layers (right rectangle and vertical figures)?
How can I merge this to make mask as in left clean rectangle?
答案1
得分: 1
以下是您要求的部分翻译:
"I don't understand what you are trying to do, but want to show you how the 4 channels (RGBA) of your image look. R is on the left, then G, then B with A (alpha/transparency) on the right.
I guess you just want the rightmost (A) channel, so with PIL, that is:
from PIL import Image
im = Image.open('....')
alpha = im.getchannel('A')
If you want all the channels, use:
R, G, B, A = im.split()
英文:
I don't understand what you are trying to do, but want to show you how the 4 channels (RGBA) of your image look. R is on the left, then G, then B with A (alpha/transparency) on the right.
I guess you just want the rightmost (A) channel, so with PIL, that is:
from PIL import Image
im = Image.open('....')
alpha = im.getchannel('A')
If you want all the channels, use:
R, G, B, A = im.split()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论