blob: 8a8ac4d4b297e6c0ef634f2fe11c27ac9734392b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
---
date: 2020-01-14 00:10
description: Short code snippet for converting between PIL image and NumPy arrays.
tags: Code-Snippet
---
# Converting between image and NumPy array
```python
import numpy
import PIL
# Convert PIL Image to NumPy array
img = PIL.Image.open("foo.jpg")
arr = numpy.array(img)
# Convert array to Image
img = PIL.Image.fromarray(arr)
```
## Saving an Image
```python
try:
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
except IOError:
PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
```
|