Since you never answered my final follow-on question in the comments, I've made a few guesses and implemented something to illustrate how it might be done using only the PIL
(or pillow
) module.
In a nutshell, the code converts the image a grayscale, divides up the resulting 0% to 100% luminosity (intensity) pixel range into equally sized sub-ranges, and then assigns a color from a palette to each of them.
from PIL import Image
from PIL.ImageColor import getcolor, getrgb
from PIL.ImageOps import grayscale
try:
xrange
except NameError: # Python 3.
xrange = range
def falsecolor(src, colors):
if Image.isStringType(src): # File path?
src = Image.open(src)
if src.mode not in ['L', 'RGB', 'RGBA']:
raise TypeError('Unsupported source image mode: {}'.format(src.mode))
src.load()
# Create look-up-tables (luts) to map luminosity ranges to components
# of the colors given in the color palette.
num_colors = len(colors)
palette = [colors[int(i/256.*num_colors)] for i in xrange(256)]
luts = (tuple(c[0] for c in palette) +
tuple(c[1] for c in palette) +
tuple(c[2] for c in palette))
# Create grayscale version of image of necessary.
l = src if Image.getmodebands(src.mode) == 1 else grayscale(src)
# Convert grayscale to an equivalent RGB mode image.
if Image.getmodebands(src.mode) < 4: # Non-alpha image?
merge_args = ('RGB', (l, l, l)) # RGB version of grayscale.
else: # Include copy of src image's alpha layer.
a = Image.new('L', src.size)
a.putdata(src.getdata(3))
luts += tuple(xrange(256)) # Add a 1:1 mapping for alpha values.
merge_args = ('RGBA', (l, l, l, a)) # RGBA version of grayscale.
# Merge all the grayscale bands back together and apply the luts to it.
return Image.merge(*merge_args).point(luts)
if __name__ == '__main__':
R, G, B = (255, 0, 0), ( 0, 255, 0), ( 0, 0, 255)
C, M, Y = ( 0, 255, 255), (255, 0, 255), (255, 255, 0)
filename = 'test_image.png'
# Convert image into falsecolor one with 4 colors and display it.
falsecolor(filename, [B, R, G, Y]).show()
Below is a composite showing an RGB test image, the intermediate internal 256-level grayscale image, and the final result of changing that into a false color one comprised of only four colors (each representing a range of 64 levels of intensity):

Here's another composite, only this time showing the conversion of an image that's already grayscale into the same palette of 4 false colors.

Is something like this what you're wanting to do?