Use plt.imshow to create the figure, and plt.show to display it:
from matplotlib import pyplot as plt
plt.imshow(data, interpolation='nearest')
plt.show()
For Jupyter notebooks, add this line before importing matplotlib:
%matplotlib inline
For interactive plots in Jupyter [demo], install ipyml pip install ipympl, then use:
%matplotlib widget
Answer from Steve Tjoa on Stack OverflowUse plt.imshow to create the figure, and plt.show to display it:
from matplotlib import pyplot as plt
plt.imshow(data, interpolation='nearest')
plt.show()
For Jupyter notebooks, add this line before importing matplotlib:
%matplotlib inline
For interactive plots in Jupyter [demo], install ipyml pip install ipympl, then use:
%matplotlib widget
You could use PIL to create (and display) an image:
from PIL import Image
import numpy as np
w, h = 512, 512
data = np.zeros((h, w, 3), dtype=np.uint8)
data[0:256, 0:256] = [255, 0, 0] # red patch in upper left
img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.show()
Converting between PIL Image and 2d pixel array?
How to convert RGB array to image?
Numpy array to image data
How to convert array to image in python? - Stack Overflow
I need to do the following:
-
Import an image.
-
Convert image to a 2D array of pixel values (rgb tuples, bytes, whatever it doesn't matter).
-
Manipulate that 2D array (needs to be 2D as I'm using a library that requires a 2D array of data).
-
Convert the newly changed 2D array back into an image.
-
Save the image.
Anyone know how I can accomplish this? PIL Image documentation has some functions to get data but it's all 1D arrays, and I'm not sure how to get them into 2D arrays and then re-convert it back into an image again.
So I tried to convert a raw RGB array to an image with this simple code:
import numpy as np
from PIL import Image
Canvas = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 0], [0, 0, 0]])
img = Image.fromarray(Canvas, 'RGB')
img = img.save("canvas.png")
I have no idea how to set the size of the image as a resolution of 2x2 because this code does not create the correct resolution
The first pixel in Canvas should be red, second pixel should be green, the third and fourth pixels should be black
All I want is a 2x2 resolution image where the top-left pixel is red and the top-right pixel is green, then both bottom pixels should be black, but my code is just producing the wrong image resolution with the wrong pixel locations. What is the issue?