You can tell which mode your image has by looking at:
image.mode
If your image is palettised it will be P
, or PA
if palettised with an alpha channel as well. If your image is greyscale it will be L
, or LA
if greyscale with alpha channel.
You convert between them using the convert(mode) function, e.g. to go to RGB mode, use:
image.convert('RGB')
I used the word "normally" quite a lot! Why? Because you can do abnormal things!
You can store a grey-looking image in an RGB format. All you do, is make the red component equal to the green component equal to the blue component (R=G=B) and it will appear grey but be stored in an inefficient RGB format that takes 3x the space it might otherwise need to.
You can store a grey-looking image in a P format, you just make sure all the palette entries have the R=G=B.
Here's the kicker... if you want and expect an RGB image, you should just convert to RGB on opening:
im = Image.open("image.jpg").convert('RGB')
that way you will never have problems with GIF files (which are always palettised) nor with PNG files which can be palettised and can be greyscale or RGB. You will not normally get problems with JPEG images because they are pretty much always RGB anyway.
Here's an example to demonstrate. Start with this red-blue gradient image:
Let's use IPython
to look at in RGB space. First, look at the Red channel:
In [21]: im = Image.open('a.png').convert('RGB')In [22]: np.array(im.getchannel(0))Out[22]: array([[255, 255, 255, ..., 255, 255, 255],[255, 255, 255, ..., 255, 255, 255],[254, 254, 254, ..., 254, 254, 254],...,[ 1, 1, 1, ..., 1, 1, 1],[ 0, 0, 0, ..., 0, 0, 0],[ 0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
Notice it has 255 at the top because it is red, and 0 at the bottom because there is no red there.
Now let's look at the Green channel, it is 0 everywhere because there is no green.
In [23]: np.array(im.getchannel(1))Out[23]: array([[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],...,[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
And finally, let's look at the Blue channel. It is 0 at the top where the image is pure Red and 255 at the bottom where the image is pure Blue.
In [24]: np.array(im.getchannel(2))Out[24]: array([[ 0, 0, 0, ..., 0, 0, 0],[ 0, 0, 0, ..., 0, 0, 0],[ 1, 1, 1, ..., 1, 1, 1],...,[254, 254, 254, ..., 254, 254, 254],[255, 255, 255, ..., 255, 255, 255],[255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
Now let's look at the same image in palette mode.
# Convert to palette modeim = Image.open('a.png').convert('P')# Extract the palette and reshape as 256 entries of 3 RGB bytes eachIn [27]: np.array(im.getpalette()).reshape(256,3)Out[27]: array([[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 0, 0, 0],[ 51, 0, 0],[102, 0, 0],[153, 0, 0],[204, 0, 0],[255, 0, 0], <--- entry 15 = rgb(255,0,0) = Red[ 0, 51, 0],[ 51, 51, 0],[102, 51, 0],[153, 51, 0],[204, 51, 0],[255, 51, 0],[ 0, 102, 0],[ 51, 102, 0],[102, 102, 0],[153, 102, 0],[204, 102, 0],[255, 102, 0],[ 0, 153, 0],[ 51, 153, 0],[102, 153, 0],[153, 153, 0],[204, 153, 0],[255, 153, 0],[ 0, 204, 0],[ 51, 204, 0],[102, 204, 0],[153, 204, 0],[204, 204, 0],[255, 204, 0],[ 0, 255, 0],[ 51, 255, 0],[102, 255, 0],[153, 255, 0],[204, 255, 0],[255, 255, 0],...... up to 256 entries
Now get the indices into the palette:
In [28]: np.array(im.getchannel(0))Out[28]: array([[ 15, 15, 15, ..., 15, 15, 15],[ 15, 15, 15, ..., 15, 15, 15],[ 15, 15, 15, ..., 15, 15, 15],...,[190, 190, 190, ..., 190, 190, 190],[190, 190, 190, ..., 190, 190, 190],[190, 190, 190, ..., 190, 190, 190]], dtype=uint8)
Now you can see that the top row of the image has palette index 15, which, if you look it up in the preceding palette, you will see is Red.
Now let's look at the same image in L mode - remember L means "Luminance" which is just a fancy way of saying "brightness" on a scale of black to white, i.e. greyscale :
# Open into greyscale, or L modeIn [1]: im = Image.open('a.png').convert('L')# Dump the pixelsIn [2]: np.array(im.getchannel(0))Out[2]: array([[76, 76, 76, ..., 76, 76, 76],[76, 76, 76, ..., 76, 76, 76],[76, 76, 76, ..., 76, 76, 76],...,[29, 29, 29, ..., 29, 29, 29],[29, 29, 29, ..., 29, 29, 29],[29, 29, 29, ..., 29, 29, 29]], dtype=uint8)
So, now the top row of the image is 76 and the bottom row is 29. What are those? Well, the formula for converting RGB to L is:
L = R * 299/1000 + G * 587/1000 + B * 114/1000
So, in the top row, R=255, G=0, B=0, so the Luminance has become:
L = 255 * 299/1000 + 0 + 0 L = 76
And on the bottom row, R=0, G=0, B=255, so the Luminance has become:
L = 0 + 0 + 255 * 114/1000L = 29
Keywords: Python, PIL, Pillow, palette, image processing, prime.
"L" mode maps to black and white pixels (and in between). "P" mode maps with a color palette. You can convert image to one of these modes.
from PIL import Imageim = Image.open("im.jpg")im_l = im.convert('L')im_p = im.convert('P')im.show()im_l.show()im_p.show()
The "L" mode represents grayscale here.... So it can hold any of 256 shades of Gray (includes black and white as Gray shades).
The "P" mode can hold 256 different colors like red,blue, green etc....
Convertion from one another, if you mean converting images from grayscale to color or vice versa.... Yes it is possible....
Examples: 8 bit black and white image ( technically Gray scale image)are "L" and any 8 bit color images are "P" mode..