【问题标题】:Pillow Fully Transparent Image Black not Transparrent枕头全透明图像黑色不透明
【发布时间】:2017-06-04 15:20:37
【问题描述】:

我正在尝试在 Python 中使用 Pillow 制作完全透明的图像

img2 = PILImage.new('RGBA', (1920, 1080), (0,0,0,0))
img2.save('test.png')

上面的代码是参考创建的: https://pillow.readthedocs.io/en/4.0.x/reference/Image.html#constructing-images

PIL.Image.new(mode, size, color=0)

color – 图像使用什么颜色。默认为黑色。如果给定,这应该是单波段模式的单个整数或浮点值,以及多波段模式的元组(每个波段一个值)。创建 RGB 图像时,您还可以使用 ImageColor 模块支持的颜色字符串。如果颜色为无,则图像未初始化。

https://pillow.readthedocs.io/en/4.0.x/handbook/concepts.html#concept-modes

RGBA(4x8 位像素,带有透明蒙版的真彩色) 上面的代码生成 test.png 一个黑色的图像而不是一个完全透明的图像:

系统信息:

$ pip2.7 freeze | grep -i pillow
Pillow==4.0.0

$ python2.7 -V
Python 2.7.9

$ apt list --installed | grep -i imagemagick    
imagemagick/stable,now 8:6.8.9.9-5+deb8u6 amd64 [installed]
imagemagick-6.q16/stable,now 8:6.8.9.9-5+deb8u6 amd64 [installed,automatic]
imagemagick-common/stable,now 8:6.8.

$ convert --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-11-26 http://www.imagemagick.org

$ uname -a
Linux revo-vpn-74 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux

$ cat /etc/*release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

【问题讨论】:

  • 在我的应用程序之外测试下面的代码问题,结果与 Hugo 发布的一样

标签: python python-2.7 imagemagick pillow


【解决方案1】:

它对我有用(macOS Sierra / Python 2.7.13 / Pillow 4.0.0)(注意导入和Image.new 而不是PILImage.new):

from PIL import Image
img2 = Image.new('RGBA', (1920, 1080), (0,0,0,0))
img2.save('test.png')

这是一个不透明的:

img3 = Image.new('RGB', (1920, 1080), (0,0,0))
img3.save("test3.png")

【讨论】:

  • 它也对我有用。请注意,某些图像查看器将透明颜色显示为黑色。您使用的是哪个图像查看器?如果您选择红色 (255,0,0,0),您是否看到相同的颜色?使用支持透明度的图像格式也很重要,但在您的示例中,您使用的是 png,所以应该没问题。
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 2016-06-14
  • 2021-10-08
  • 1970-01-01
  • 2013-08-21
  • 2015-08-12
相关资源
最近更新 更多