summaryrefslogtreecommitdiff
path: root/posts/2020-01-14-Converting-between-PIL-NumPy/index.html
blob: fb685a248dd361cef608adc3c700d1191f1559e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><meta name="og:site_name" content="Navan Chauhan"/><link rel="canonical" href="https://navanchauhan.github.io/posts/2020-01-14-Converting-between-PIL-NumPy"/><meta name="twitter:url" content="https://navanchauhan.github.io/posts/2020-01-14-Converting-between-PIL-NumPy"/><meta name="og:url" content="https://navanchauhan.github.io/posts/2020-01-14-Converting-between-PIL-NumPy"/><title>Converting between image and NumPy array | Navan Chauhan</title><meta name="twitter:title" content="Converting between image and NumPy array | Navan Chauhan"/><meta name="og:title" content="Converting between image and NumPy array | Navan Chauhan"/><meta name="description" content="Short code snippet for converting between PIL image and NumPy arrays."/><meta name="twitter:description" content="Short code snippet for converting between PIL image and NumPy arrays."/><meta name="og:description" content="Short code snippet for converting between PIL image and NumPy arrays."/><meta name="twitter:card" content="summary"/><link rel="stylesheet" href="/styles.css" type="text/css"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><link rel="shortcut icon" href="/images/favicon.png" type="image/png"/><link rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan"/><meta name="twitter:image" content="https://navanchauhan.github.io/images/logo.png"/><meta name="og:image" content="https://navanchauhan.github.io/images/logo.png"/></head><body class="item-page"><header><div class="wrapper"><a class="site-name" href="/">Navan Chauhan</a><nav><ul><li><a class="selected" href="/posts">Posts</a></li><li><a href="/publications">Publications</a></li><li><a href="/about">About Me</a></li><li><a href="https://navanchauhan.github.io/repo">Repo</a></li></ul></nav></div></header><div class="wrapper"><article><div class="content"><span class="reading-time">🕑 0 minute read.</span><h1>Converting between image and NumPy array</h1><pre><code>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)
</code></pre><h2>Saving an Image</h2><pre><code>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)
</code></pre></div><span>Tagged with: </span><ul class="tag-list"><li><a href="/tags/codesnippet">code-snippet</a></li><li><a href="/tags/tutorial">tutorial</a></li></ul></article></div><footer><p>Made with ❤️ using <a href="https://github.com/johnsundell/publish">Publish</a></p><p><a href="/feed.rss">RSS feed</a></p></footer></body></html>