I am starting a little project and I am having some difficulty in finding the answer that I am looking for. I don't really know exactly what terms I should be using to search, and couldn't find anything similar so I am sorry if this has been asked previously.
I am essentially trying to have a 2D plot of a set size, 300x300 for example full of random RGB pixels. I have figured out how to plot them with imshow and remove the axis labels so far, but it looks odd like it is zoomed in too far. Also, I know I can use RGB arguements in imshow, but the matplotlib manual touches on it, but never gives any examples. The closest cmap I have found to RGB is hsv so I am using that for now until I find the RGB solution.
Can anyone help me with assigning a random RGB value to each pixel instead of using cmap, and maybe adjusting the apparent size of the image so the pixels are less "zoomed in"? I am open to using something other than imshow for flexibility, it is just the only thing I found to do what I want. Thank you very much in advance!
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from numpy import random
Z = random.random((300,300))
plt.imshow(Z, cm.get_cmap("hsv"), interpolation='nearest')
plt.axis('off')
plt.show()
CodePudding user response:
Here's how you do it with PIL:
from PIL import Image
import numpy as np
data = (np.random.random( (300,300,3) ) * 256).astype(np.uint8)
img = Image.fromarray(data)
img.show()
img.save('rand.png')