Sure. Luminance is just another word for grayscale. i.e. it's an array with a single (0-255) value at each pixel x,y location, so 2D. With PIL you can produce this using Image.convert('L') as in the example I posted. (I did it as the first operation as it's very quick to do and reduces the work of the subsequent Image.resize() by two thirds)

If you already have your image in the form of a numpy ndarray you can convert it by

Code: Select all

` im = (im[:,:,:3] * [0.2989, 0.5870, 0.1140]).sum(axis=2)`

which in words would be:

take a slice of the image using all the x and y values but only the first three values of the RGBA axis,

multiply the R value by 0.2989, G x 0.5870, B x 0.114 (which is a good approx to how our eyes perceive relative colours)

finally sum the values along the 3rd axis (axis numbers start at zero 0=>y, 1=>x, 2=>RGBA)

To find the difference between two images in RGB space you 'simply' use Pythagoras. i.e. square root of (red distance squared + green distance squared + blue distance squared). In numpy:

Code: Select all

```
diff = ((im1[:,:,0] - im2[:,:,0]) ** 2 + (im1[:,:,1] - im2[:,:,1]) ** 2 + (im1[:,:,2] - im2[:,:,2]) ** 2) ** 0.5
# diff.sum() # aggregates the total of differences
```

But the histogram code wasn't doing this as Image.histogram() produces a 1D array laid out as the number of pixels with red value 0 then 1 then 2...255 followed by the number of pixels with green value 0, 1, 2 etc