0
A
回答
1
# Open the image and convert it to RGBA, just in case it was indexed
image = Image.open(image_path).convert('RGBA')
# Extract just the alpha channel
alpha = image.split()[-1]
# Unfortunately the alpha channel is still treated as such and can't be dumped
# as-is
# Create a new image with an opaque black background
bg = Image.new("RGBA", image.size, (0,0,0,255))
# Copy the alpha channel to the new image using itself as the mask
bg.paste(alpha, mask=alpha)
# Since the bg image started as RGBA, we can save some space by converting it
# to grayscale ('L') Optionally, we can convert the image to be indexed which
# saves some more space ('P') In my experience, converting directly to 'P'
# produces both the Gray channel and an Alpha channel when viewed in GIMP,
# althogh the file sizes is about the same
bg.convert('L').convert('P', palette=Image.ADAPTIVE, colors=8).save(
mask_path,
optimize=True)
相关问题
- 1. Java:从BufferedImage中提取Alpha通道
- 2. 如何使用IImgCtx获取带有Alpha通道的图像?
- 3. 如何从PHP中的图像检索Alpha通道?
- 4. 如何添加alpha通道到该图像使用ImageMagick
- 5. 如何使用PIL/Pillow读取zip文件中的图像
- 6. 如何在J2ME中从PNG中读取原始alpha通道?
- 7. 如何知道图像是“预乘Alpha”?
- 8. 从UIImage矩形获取alpha通道
- 9. 使用Python成像库(PIL),如何在另一个图像上使用Alpha通道组成图像?
- 10. 仅使用图像的SVG蒙版alpha通道
- 11. 如何使用opencv python将图像的alpha通道转换为白色?
- 12. 如何使用图像4个通道
- 13. 如何使用Java(不使用pdfbox)从pdf中提取图像
- 14. 如何从C#中的位图插入/提取透明通道?
- 15. 使用PIL/PILLOW写在图像上
- 16. 使用C从PDF中提取图像#
- 17. 使用pdfbox从PDF中提取图像
- 18. 使用java从mp4中提取图像
- 19. 如何使用AppleScript从批量图像中删除Alpha
- 20. Windows Mobile(MFC)。如何将图像从CImageList转换为CBitmap的alpha通道?
- 21. 如何从HBITMAP创建Gdiplus ::位图,保留Alpha通道信息?
- 22. 如何在不丢失.NET中的alpha通道的情况下从剪贴板中获取图像?
- 23. 确定是否alpha通道,在影像中使用
- 24. 在Mathematica中轻松去除图像中的alpha通道
- 25. 如何从图像中提取背景?
- 26. 如何从URL中提取图像
- 27. 如何从excel表中提取图像
- 28. 如何从Access CommandButton中提取图像?
- 29. 如何从图像中提取折线
- 30. 如何从图像中提取脸部?
对于'bg = Image.new(“RGBA”,image.size,(0,0,0,255)'',你缺少一个parentesis' –