Have a nice day
Have a nice day

Reputation: 1015

Why is PIL.ImageColor.getcolor() only returning white or black?

I have this code:

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

taken from this answer.

Running this produces a 1 pixel image that is black. If I change the color to red, for example:

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

I get a white pixel instead of red. I've tried yellow, green blue; they all result in a white pixel. How can I fix this?

Upvotes: 1

Views: 960

Answers (1)

Have a nice day
Have a nice day

Reputation: 1015

I had the wrong mode:

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

I replaced the '1' with 'RGB' and now it works.

Upvotes: 2

Related Questions