【问题标题】:Why is PIL.ImageColor.getcolor() only returning white or black?为什么 PIL.ImageColor.getcolor() 只返回白色或黑色?
【发布时间】:2021-05-11 00:10:31
【问题描述】:

我有这个代码:

from PIL import Image, ImageColor
i = Image.new('1', (1, 1))
i.putpixel((0, 0), ImageColor.getcolor('black','1'))
i.save('simplePixel.png')

取自answer

运行此程序会生成一个 1 像素的黑色图像。如果我将颜色更改为红色,例如:

ImageColor.getcolor('red','1')

我得到一个白色像素而不是红色。我试过黄色,绿色蓝色;它们都会产生一个白色像素。我该如何解决这个问题?

【问题讨论】:

    标签: python python-imaging-library


    【解决方案1】:

    我的模式有误:

    from PIL import Image, ImageColor
    i = Image.new('RGB', (1, 1))
    i.putpixel((0, 0), ImageColor.getcolor('red','RGB'))
    i.save('simplePixel.png')
    

    我用'RGB' 替换了'1',现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2011-02-26
      • 2014-06-02
      相关资源
      最近更新 更多