如何解决PNG图像中的透明区域问题?

huangapple go评论77阅读模式
英文:

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:

如何解决PNG图像中的透明区域问题?

Mask png:

如何解决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.

如何解决PNG图像中的透明区域问题?

Mask png.

如何解决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.

如何解决PNG图像中的透明区域问题?

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.

如何解决PNG图像中的透明区域问题?

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()

huangapple
  • 本文由 发表于 2023年2月19日 15:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定