0

I'm using the python imaging library to build a bmp of some data. However, I really need to change my color using decimal numbers. The range of colors from int 0 to int 256 isn't enough variation for my diagram.

Here's an example to show somthing similar to what I'm doing:

pixels[x,y] = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256))

#note: in my example, the random int is the same for each param

however, I would want to vary my pixels from something larger (maybe like 0 - 1000). I wanted to use decimals so that I could vary the color within the 0 - 256 limit. However, I get an error when I paint a pixel with a decimal number. Is there a work-around for this?

DannyD
  • 2,732
  • 16
  • 51
  • 73
  • 2
    16,777,216 colors isn't enough for you? What kind of image is this? – jwodder Dec 03 '13 at 23:02
  • I'm just using a greyscale image so the variation from 0 -256 isn't enough – DannyD Dec 03 '13 at 23:03
  • I don't want to use other colors for this project. thanks – DannyD Dec 03 '13 at 23:03
  • Please read some article about, what RGB means. – fuesika Dec 03 '13 at 23:03
  • yes, I understand what RGB is. However, I'm changing my colors to get different greys. So my RGB values would each be the same. For example: (0,0,0) or (100,100,100) or (256,256,256) – DannyD Dec 03 '13 at 23:07
  • 2
    I don't think you want more values so much as a method for generating dissimilar colors. Consult http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette or http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/, and also look into HSL or HSV. – Waleed Khan Dec 03 '13 at 23:08
  • PIL doesn't support formats with more than 8 bits of greyscale. You'll need to find something else. – Ignacio Vazquez-Abrams Dec 03 '13 at 23:28

2 Answers2

0

No. Pixel values in the formats supported by PIL can only be integers; if you want to use fractional values then you will need to scale them to their appropriate integer equivalent, or use a module such as PyCairo that will translate them for you.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Yes. (sort of)

For greyscale, the usual PIL color mode is "L", that is 8 bit per pixel, but you can also use mode "I" 32-bit integer pixels, or "F" 32-bit float pixel.

img = new('F', (100, 100))
img.putpixel( (40, 60), 100.5)
print img.getpixel( (40, 60))
# 100.5

However, you will have a hard time with PIL to save this to a format without conversion to a 8bpp color mode. Tiff format can support 16 or 32 bits per pixel per plane, but PIL don't support this yet.

So You won't really be able to really see the result. Your eye is probably not able to perceive the difference anyway.

MatthieuW
  • 2,292
  • 15
  • 25