Reputation: 309
I'm trying to make a 100x100
image with each pixel being a different random colour, like this example:
I've tried to use matplotlib
but I'm not having much luck. Should I maybe be using PIL?
Upvotes: 19
Views: 60779
Reputation: 63709
I wanted to write some simple BMP files, so I researched the format and wrote a very simple bmp.py module:
# get bmp.py at http://www.ptmcg.com/geo/python/bmp.py.txt
from bmp import BitMap, Color
from itertools import product
from random import randint, choice
# use a set to make 256 unique RGB tuples
rgbs = set()
while len(rgbs) < 256:
rgbs.add((randint(0,255), randint(0,255), randint(0,255)))
# convert to a list of 256 colors (all you can fit into an 8-bit BMP)
colors = [Color(*rgb) for rgb in rgbs]
bmp = BitMap(100, 100)
for x,y in product(range(100), range(100)):
bmp.setPenColor(choice(colors))
bmp.plotPoint(x, y)
bmp.saveFile("100x100.bmp", compress=False)
Sample 100x100.bmp:
For a slightly larger pixel size, use:
PIXEL_SIZE=5
bmp = BitMap(PIXEL_SIZE*100, PIXEL_SIZE*100)
for x,y in product(range(100), range(100)):
bmp.setPenColor(choice(colors))
bmp.drawSquare(x*PIXEL_SIZE, y*PIXEL_SIZE, PIXEL_SIZE, fill=True)
filename = "%d00x%d00.bmp" % (PIXEL_SIZE, PIXEL_SIZE)
bmp.saveFile(filename)
You may not want to use bmp.py, but this shows you the general idea of what you'll need to do.
Upvotes: 6
Reputation: 27575
If you want to create an image file (and display it elsewhere, with or without Matplotlib), you could use NumPy and Pillow as follows:
import numpy
from PIL import Image
imarray = numpy.random.rand(100,100,3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
im.save('result_image.png')
The idea here is to create a numeric array, convert it to a RGB image, and save it to file. If you want grayscale image, you should use convert('L')
instead of convert('RGBA')
.
Upvotes: 38
Reputation: 21
I believe the colour map of that array to be bone, i.e.
#import the modules
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
rand_array=np.random.rand(550,550) #create your array
plt.imshow(rand_array,cmap=cm.bone) #show your array with the selected colour
plt.show() #show the image
Change 550 to 100 if you want a 100x100 array
Upvotes: 2
Reputation: 2505
import numpy as np
import matplotlib.pyplot as plt
img = (np.random.standard_normal([28, 28, 3]) * 255).astype(np.uint8)
# see the raw result (it is 'antialiased' by default)
_ = plt.imshow(img, interpolation='none')
# if you are not in a jupyter-notebook
plt.show()
Will give you this 28x28 RGB image:
Upvotes: 6
Reputation: 88128
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()
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()
Upvotes: 28