Make a color grid with different colors for each index

Viewed 285

Ideally using Numpy and PIL, I'd like to make a grid of colors for a image segmentation program I'm making. Not all of the colors have to be unique, but, each color should be distinct from its neighbors and recognizable for its position. The idea is to create an overlay for which each pixel's x,y position coordinates can be quickly discerned. The images are 250x250.

1 Answers

Okay, I messed around with it and came up with something like this:

import PIL.Image

def zoom(img, zoom):
    w, h = img.size
    print(w,h)
    zoom2 = zoom * 2
    return img.resize((w*zoom, h*zoom), PIL.Image.NEAREST)

green = 0
red = 0
blue = 0

img = np.zeros([10,10,3], dtype=np.uint8)

for i in range(10):
    green += 52
    blue += 45
    green %= 256
    blue %= 256
    img[i,:,1:] = [blue,green]
    
blue = 0
for j in range(10):
    red += 47
    blue += 48
    red %= 256
    blue %= 256
    img[:,j,0] = red
    img[:,j,2] = blue

grid = np.tile( img, [5, 5, 1])
grid = grid.astype(np.uint8)
zoom(PIL.Image.fromarray(grid), 8)

Resulting in this picture:

grid of colors

This only makes a 50x50 grid, but it's pretty hard to see the grid on a larger image. I might try doing my segmentation program another way. One nice thing about this is that you can "debug" half of the colors by commenting out either the code that sets the x color values, or the code that sets the y color values.

Related