diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2021-05-26 23:58:29 +0530 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2021-05-26 23:58:29 +0530 |
commit | bfd3a825c2d73bd842769cdfaf11ad0319a3bd6e (patch) | |
tree | 7b2c052bdf539f433ed3ab6bd133b6d46c7ff7e5 /Content/posts/2020-01-14-Converting-between-PIL-NumPy.md | |
parent | 2cb28c0dd749611e6edd4688955769bda3381453 (diff) |
added code and content
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) +``` |