summaryrefslogtreecommitdiff
path: root/Content/posts/2020-01-14-Converting-between-PIL-NumPy.md
blob: d1f502bc70acd603cedf08a809b3235effeca7bd (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, Tutorial
---

# 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)
```