How to effectively do a color mapping?

82 Views Asked by At

Given an image I'd like to map each pixel color to a different color. Currently, I'm taking each pixel and comparing against some color ranges, and if the pixel color lays inside that given range, I switch the color.

def generate(picture, colors):
    ranges = [127, 254, 381, 509, 636, 766]
    w, h = picture.size

    for i in range(w):
        for j in range(h):
            r, g, b = picture.getpixel((i, j))
            current_color = r + g + b
            for k in range(len(ranges)):
                if current_color < ranges[k]:
                    best_color = colors[k]
                    picture.putpixel((i, j), best_color)
                    break

    return picture

picture = Image.open("images\\image.jpg")
colors = [(0,0,0), (0,0,128), (0,0,255), (77,77,255), (153,153,255), (255,255,255)]
picture = generate(picture,colors)
picture.show()

It seems to be working and creating a good image, but is there a way to make it look even closer to the original? Perhaps by changing the range intervals to not be uniform?

0

There are 0 best solutions below