Python array convert to image
时间: 2023-12-31 21:06:32 浏览: 28
To convert a Python array into an image, you can use the Pillow library. Here's an example code that shows how to do it:
```
from PIL import Image
import numpy as np
# Create a 2D numpy array
data = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]])
# Convert the array to an image
img = Image.fromarray(data, 'RGB')
# Save the image to disk
img.save('output.png')
```
In this code, we first create a 2D numpy array `data` that represents the image. Each element of the array represents a pixel in the image, and its value determines the color of the pixel.
We then use the `Image.fromarray()` method to convert the numpy array into a Pillow image. The second argument ('RGB') specifies the color mode of the image.
Finally, we save the image to disk using the `save()` method. The resulting image will be a PNG file named 'output.png' that shows a red, green, and blue pixel in a diagonal line.