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. 使用Alpha通道获取CGPath图像
- 2. Java:从BufferedImage中提取Alpha通道
- 3. 如何使用IImgCtx获取带有Alpha通道的图像?
- 4. 从图像中提取RGB通道
- 5. 如何在Python OpenCV中读取TIFF图像的Alpha通道?
- 6. 如何从PHP中的图像检索Alpha通道?
- 7. 我可以通过alpha通道从图像获取CGPath吗?
- 8. 如何使用PHP Gmagick从PNG文件中提取alpha和其他通道?
- 9. 如何添加alpha通道到该图像使用ImageMagick
- 10. 如何使用PIL/Pillow读取zip文件中的图像
- 11. 带“alpha”通道的“直”版图像
- 12. Win32剪贴板和alpha通道图像
- 13. 添加Alpha通道的图像与PerlMagick
- 14. 如何在J2ME中从PNG中读取原始alpha通道?
- 15. 如何知道图像是“预乘Alpha”?
- 16. 从存储为Numpy阵列的图像中丢弃Alpha通道
- 17. 从PDF中删除图像(带有透明度/ alpha通道)
- 18. 如何暂时用PILLOW转换图像?
- 19. 从UIImage矩形获取alpha通道
- 20. 使用Python成像库(PIL),如何在另一个图像上使用Alpha通道组成图像?
- 21. alpha通道如何为-1?
- 22. 使用Graphics.DrawImage()绘制透明度/ Alpha通道图像
- 23. 是否将ARGB_8888加载JPEG图像时使用了alpha通道?
- 24. 仅使用图像的SVG蒙版alpha通道
- 25. 使用CIFilters时获取Alpha通道[如何清除]
- 26. 如何使用opencv python将图像的alpha通道转换为白色?
- 27. 如何使背景PNG图像alpha通道在QMainWindow中显示为透明
- 28. 最快的方式alpha通道应用到图像中的OpenCV
- 29. 如何将图像合并到使用PIL/Pillow的画布中?
- 30. 如何使用图像4个通道
对于'bg = Image.new(“RGBA”,image.size,(0,0,0,255)'',你缺少一个parentesis' –