[Image-SIG] Converting from 24-bit RGB to Black&White

Fredrik Lundh fredrik at pythonware.com
Thu Mar 2 23:23:43 CET 2006


Alexander Belchenko wrote:

> I don't understand why solid green color (#00FF00) converted as mix of
> black and white dots (you can see this in my out1.bmp). Is this result
> of dithering?

yes.

when converted to luminance (grayscale), green corresponds to
a gray level, not white or black:

>>> import Image
>>> i = Image.new("RGB", (1,1), "#00FF00")
>>> i.getpixel((0,0))
(0, 255, 0)
>>> i = i.convert("L")
>>> i.getpixel((0,0))
149

to represent149 in an image that only consists of 0 (black) and
255 (white), PIL uses a dither pattern to make the image look gray,
on average:

>>> i = Image.new("RGB", (100,100), "#00FF00")
>>> i = i.convert("1")
>>> i.getcolors()
[(4140, 0), (5860, 255)]
>>> (4140*0+5860*255)/(100*100)
149

> And why paletted image works as described in 1.1.3 manual?

conversion to P uses dithering too, so it's probably only your choice
of colors that makes things work as you want when you do this.

if you want full control of the graylevel-to-bilevel mapping, and you
don't want dithering, I'd recommend converting first to "L", and then
using the point method with a suitable lookup table to convert to
"1":

   im = im.convert("L") # get luminance
   im = im.point(table, "1")

</F>





More information about the Image-SIG mailing list