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

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

How to solve problem with transparent area in PNG image?

问题

I use Paint.Net in Windows to make mask png image from source png.

  1. def mask(im):
  2. newimdata = []
  3. transparent = (255, 255, 255, 0)
  4. black = (0, 0, 0)
  5. white = (255, 255, 255)
  6. for color in im.getdata():
  7. if color == transparent:
  8. newimdata.append(white)
  9. else:
  10. newimdata.append(black)
  11. newim = Image.new(im.mode, im.size)
  12. newim.putdata(newimdata)
  13. return newim
  14. img = Image.open(thumb)
  15. img = img.convert("RGBA")
  16. 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.

  1. def mask (im):
  2. newimdata = []
  3. transparent = (255, 255, 255, 0)
  4. black = (0,0,0)
  5. white = (255,255,255)
  6. for color in im.getdata():
  7. if color == transparent:
  8. newimdata.append(white)
  9. else:
  10. newimdata.append(black)
  11. newim = Image.new(im.mode,im.size)
  12. newim.putdata(newimdata)
  13. return newim
  14. img = Image.open(thumb)
  15. img = img.convert("RGBA")
  16. 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:

  1. from PIL import Image
  2. im = Image.open('....')
  3. alpha = im.getchannel('A')

If you want all the channels, use:

  1. 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:

确定