1

I have an image on which the quantize() function (PIL) was used.

def dither(im):
    colorPalette =((255, 255, 255), 
            (193, 193, 193),
            (239, 19, 11),
            (255, 113, 0),
            (255, 228, 0),
            (0, 204, 0),
            (0, 178, 255),
            (35, 31, 211),
            (163, 0, 186),
            (211, 124, 170),
            (160, 82, 45),
            (0, 0, 0),
            (76, 76, 76),
            (116, 11, 7),
            (194, 56, 0),
            (232, 162, 0),
            (0, 85, 16),
            (0, 86, 158),
            (14, 8, 101),
            (85, 0, 105),
            (167, 85, 116),
            (99, 48, 13))
    im = im.convert('RGB')
    colorPalette = list(sum(colorPalette, ()))*12
    colorPalette = colorPalette[:768]
    palimage = Image.new('P', (im.width, im.height))
    palimage.putpalette(colorPalette)
    ditheredim = im.quantize(palette=palimage)

    return ditheredim

I now want to change the values to 0, 1, 2 ... 21 so it's easier to work with. Tried this:

width, height = im.size
im = np.array(im)
for x in range(width):
    for y in range(height):
        for i in range(len(colorPalette)):
            if im[x][y] == all(colorPalette[i]):
                im[x][y] = i

.. but it doesn't work.

itsame
  • 171
  • 4
  • 14
  • Have a look here https://stackoverflow.com/a/57204807/2836621 – Mark Setchell Feb 01 '20 at 22:12
  • that has nothing to do with my question. the quantize() is working. but I want values from 0 to 21 (number of colors in my palette) instead of the rgb values for each pixel – itsame Feb 01 '20 at 22:14
  • Maybe you could explain what you are actually trying to do a little better. – Mark Setchell Feb 01 '20 at 22:19
  • I made my image the 22 images in my palette only. Now I want to have an array not with rgb color, I want it with numbers where 0 is (255, 255, 255) etc. because its easier to work with – itsame Feb 01 '20 at 22:23
  • Can you elaborate on how it doesn't work? Just glancing, you're going to have issues changing from an RGB to scalar within a numpy array. – busybear Feb 01 '20 at 22:34
  • Sorry, I can't work out what you mean. You have 22 images, which I can't see. Are they JPEG or PNG? And you want them all to use the same palette? – Mark Setchell Feb 01 '20 at 22:36
  • New try, I was a bit unclear I think. I input one image, it gets passed to the dither function where the palette gets applied. I get back an image with only the palette colors (in RGB) but I want an array where every pixel is represented with a number between 0 and 21. For example: 0 = (255, 255, 255). (255, 255, 255) is the first color in my palette. 21 would represent (99, 48, 13) which is the last color in my palette. Hope it was understandable:) – itsame Feb 01 '20 at 23:12
  • @MarkSetchell :) – itsame Feb 02 '20 at 14:35

0 Answers0