

So, now the top row of the image is 76 and the bottom row is 29. In : im = Image.open('a.png').convert('L') greyscale : # Open into greyscale, or L mode 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. 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 get the indices into the palette: In : np.array(im.getchannel(0)) # Extract the palette and reshape as 256 entries of 3 RGB bytes each Now let's look at the same image in palette mode. It is 0 at the top where the image is pure Red and 255 at the bottom where the image is pure Blue. In : np.array(im.getchannel(1))Īnd finally, let's look at the Blue channel. Now let's look at the Green channel, it is 0 everywhere because there is no green. Notice it has 255 at the top because it is red, and 0 at the bottom because there is no red there. First, look at the Red channel: In : im = Image.open('a.png').convert('RGB')Īrray(, Let's use IPython to look at in RGB space. You will not normally get problems with JPEG images because they are pretty much always RGB anyway. 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. if you want and expect an RGB image, you should just convert to RGB on opening: im = Image.open("image.jpg").convert('RGB') You can store a grey-looking image in a P format, you just make sure all the palette entries have the R=G=B. 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 an RGB format.

I used the word "normally" quite a lot! Why? Because you can do abnormal things! You convert between them using the convert(mode) function, e.g. If your image is greyscale it will be L, or LA if greyscale with alpha channel. If your image is palettised it will be P, or PA if palettised with an alpha channel as well. You can tell which mode your image has by looking at: image.mode It is very compact, but only stores a greyscale, not colour.

The L means that is just stores the Luminance. If you have an L mode image, that means it is a single channel image - normally interpreted as greyscale. The disadvantage is that it can only represent 256 unique colours - so you may get banding or artefacts. The advantage is that your image requires 1/3 of the space in memory and on disk. This confers both advantages and disadvantages. That means there is a palette with up to 256 different colours in it, and instead of storing 3 bytes for R, G and B for each pixel, you store 1 byte which is the index into the palette. If you have a P mode image, that means it is palettised. That normally means that each pixel takes 3 bytes of storage, one for red, one for green and one for blue. convert argument, you'll see that none of them have zero RMSE.Īlso note the values of the difference image, the seem to be either 0 or -1, 0 is good obviously, -1 would explain the bright white spots in my original difference image (underflowing to 255).Normally, images are RGB, which means they have 3 channels, one for red, one for green and one for blue. If you play with different values of the. From PIL import Image import cv2 from argparse import ArgumentParser import numpy as np parser = ArgumentParser()
