mozlingyu
mozlingyu

Reputation: 451

what is the meaning of ‘color’ when creating image with PIL.Image.new()?

the pillow doc says the image will be a single channel pic when the color given a single integer, however I tried several times and don’t know why the image is not grey as a single channel image will be. Here is the code:

from PIL import Image
im = Image.new(‘RGB’, [100, 100], 100) # this pic is deep red
im1 = Image.new(‘RGB’, [100, 100], 1000) # this pic is red
im2 = Image.new(‘RGB’, [100, 100], 10000) # this pic is black

So, how the integer is interpreted by PIL? Is this converted using a color palette?

Upvotes: 0

Views: 664

Answers (1)

furas
furas

Reputation: 142889

In doc Image.new I see something different.

When you use RGB then it creates 3 channels and you should use tuple (R,G,B) as color to set values in all channnels for all pixels. If you use single integer/float then it may convert it to tuple (R,G,B).

If you use L then it creates 1 channel and then you should use single integer/float to set color for all pixels

But color doesn't change number of channels.


EDIT: converting 3x8bit R,G,B to/from 24bit value

value = 1000
R = value%256
value = value//256
G = value%256
value = value//256
B = value%256

print(R,G,B)
print(R+(G*256)+(B*256*256))

Result

232 3 0
1000

Upvotes: 2

Related Questions