100x100 image with random pixel colour

43,764

Solution 1

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

Solution 2

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

Solution 3

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:

img

Solution 4

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:

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)

500x500.bmp

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

Solution 5

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

enter image description here

Share:
43,764

Related videos on Youtube

IceDragon
Author by

IceDragon

IceDragon is truthey.

Updated on November 06, 2021

Comments

  • IceDragon
    IceDragon over 2 years

    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?

  • alz
    alz about 2 years
    How can I select and use specific colors like rgbs = {(5, 99, 1), (5, 8, 3),(1, 1, 1)}?
  • PaulMcG
    PaulMcG about 2 years
    Just insert that line before the colors = [Color(*rgb) for rgb in rgbs] line
  • alz
    alz about 2 years
    I did that but the colours displayed are different from the inputs
  • PaulMcG
    PaulMcG about 2 years
    Sounds like you should open this as a new question.

Related