Image.fromarray
时间: 2023-10-16 22:29:35 浏览: 107
Image.fromarray is a method in the Python Imaging Library (PIL) that creates a new Image object from a numpy array. This method is useful when working with image data that is stored as a numpy array, as it allows you to easily convert the array into an Image object that can be manipulated and saved in various file formats.
Here is an example of how to use Image.fromarray:
```python
import numpy as np
from PIL import Image
# create a numpy array with random pixel values
arr = np.random.randint(0, 256, size=(100, 100, 3), dtype=np.uint8)
# create a new Image object from the array
img = Image.fromarray(arr)
# display the image
img.show()
```
In this example, we first create a numpy array with random pixel values using the `np.random.randint` method. We then use `Image.fromarray` to create a new Image object from the array. Finally, we use the `show` method to display the image.
阅读全文