100x100 Image With Random Pixel Colour


Answer :

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


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').

Hope this helps


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  # make a list of 256 colors (all you can fit into an 8-bit BMP) colors = [Color(randint(0,255), randint(0,255), randint(0,255))                  for i in xrange(256)]  bmp = BitMap(100,100) for x,y in product(xrange(100),xrange(100)):     bmp.setPenColor(choice(colors))     bmp.plotPoint(x,y)  bmp.saveFile("100x100.bmp", compress=False) 

Sample 100x100.bmp:

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(xrange(100),xrange(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) 

500x500.bmp

You may not want to use bmp.py, but this shows you the general idea of what you'll need to do.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable