[Image-SIG] [PIL] ImageColor.getcolor incorrect for grayscale

Tim Hatch tim at timhatch.com
Mon Jan 26 18:29:06 CET 2009


I think I've discovered a bug when using named colors, due to using  
integer division too early in getcolor, which makes it disagree with  
the color conversion done in ImagingConvert for a large number of  
colors (106 of the 147 named colors).

To start off, I'm assuming that for grays where r=g=b, that  
getcolor(name, 'L') should return the same as getcolor(name, 'RGB') 
[0].  Integer division done too early rounds down, so grays end up  
slightly too dark in grayscale modes.  Here's the line in question  
(PIL 1.1.6, ImageColor.py line 108):

         return r*299/1000 + g*587/1000 + b*114/1000

All the true grays except black (that is, darkgray, dimgray, gray,  
lightgray, and white) exhibit this issue.  The one that made me notice  
this is that 'white' means 254 instead of 255.  With the attached  
patch, it now behaves sanely, i.e.:

	Image.new('RGB', (1, 1), color).convert('L').getpixel((0, 0)) ==  
Image.new('L', (1, 1), color).getpixel((0, 0))

for all named colors, and

	ImageColor.getcolor(color, 'L') == ImageColor.getcolor(color, 'RGB')[0]

for the grays.

Tim

-------------- next part --------------
A non-text attachment was scrubbed...
Name: imagecolor-rounding.diff
Type: application/octet-stream
Size: 361 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/image-sig/attachments/20090126/25f81315/attachment.obj>
-------------- next part --------------



More information about the Image-SIG mailing list