100x100 image with random pixel colour

IceDragon picture IceDragon · Mar 7, 2013 · Viewed 33k times · Source

I'm trying to make a 100x100 image with each pixel being a different random colour, like this example:

enter image description here

I've tried to use matplotlib but I'm not having much luck. Should I maybe be using PIL?

Answer

Hooked picture Hooked · Mar 7, 2013

This is simple with numpy and pylab. You can set the colormap to be whatever you like, here I use spectral.

from pylab import imshow, show, get_cmap
from numpy import random

Z = random.random((50,50))   # Test data

imshow(Z, cmap=get_cmap("Spectral"), interpolation='nearest')
show()

enter image description here

Your target image looks to have a grayscale colormap with a higher pixel density than 100x100:

import pylab as plt
import numpy as np

Z = np.random.random((500,500))   # Test data
plt.imshow(Z, cmap='gray', interpolation='nearest')
plt.show()

enter image description here