diff options
Diffstat (limited to 'Content/posts/2020-01-14-Converting-between-PIL-NumPy.md')
-rw-r--r-- | Content/posts/2020-01-14-Converting-between-PIL-NumPy.md | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Content/posts/2020-01-14-Converting-between-PIL-NumPy.md b/Content/posts/2020-01-14-Converting-between-PIL-NumPy.md new file mode 100644 index 0000000..d1f502b --- /dev/null +++ b/Content/posts/2020-01-14-Converting-between-PIL-NumPy.md @@ -0,0 +1,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) +``` |