0

I am using PIL to make pictures pixel by pixel. I notice that while I am specifying various shades of gray to my input, the output image is always in various shades of red. For example, I would assume that the following:

im = Image.new("RGB", (100, 100), "black")
im.putpixel((1,9), ImageColor.getcolor('rgb(255,255,255)', '1'))
im.putpixel((1,1), ImageColor.getcolor('rgb(55,55,55)', '1'))
im.save('test.png')

would create one gray pixel and one white pixel, however this creates two red pixels, with the "gray" pixel being a darker shade of red (see below). Why is this happening and how can I get the expected output of a gray and white pixel.

Two red pixels, rather than a gray and white pixel

Brown Philip
  • 219
  • 1
  • 3
  • 12
  • 1
    Use `Image.new('L', (100, 100), 'black')`, check [PIL image mode](https://stackoverflow.com/questions/52307290/what-is-the-difference-between-images-in-p-and-l-mode-in-pil) for an explaining. – SrPanda Apr 17 '20 at 10:34
  • I will need to use rgb colors later, so single channel images will be insufficient – Brown Philip Apr 17 '20 at 10:55
  • If you need `rgb` type color use only the `rgb` format, `ImageColor.getcolor('rgb(55,55,55)', 'RGB')`, `L` is more flexible and ill recomend using it if there is a mix of formats – SrPanda Apr 17 '20 at 11:05

1 Answers1

1

This works for me:

im = Image.new("RGB",(100,100))
im.putpixel((1,9), (255,255,255))
im.putpixel((1,1), (55,55,55))
im.save("test.png")

Resulting Image:

TheFluffDragon9
  • 514
  • 5
  • 11
  • This works for me as well. I do not know why setting the background color changes the output so drastically, and seemingly erroneously. If you have any insight into this let me know. – Brown Philip Apr 17 '20 at 10:58
  • 1
    It still works if you set the background colour to black, I believe the issue is with the Imagecolor.getcolor part, although I don't know exactly as I usually define colours as rgb tuples. – TheFluffDragon9 Apr 17 '20 at 11:12
  • I see. Yes, the issue was the style of the string/tuple I was using rather than the background color. – Brown Philip Apr 17 '20 at 11:18