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' –