summaryrefslogtreecommitdiff
path: root/posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2020-03-02 14:06:59 +0530
committerNavan Chauhan <navanchauhan@gmail.com>2020-03-02 14:06:59 +0530
commitdde266a520b03a991e49cac94509b58e7b10e7f2 (patch)
tree1cd8d990b7b56fc1e6bac533a8b7bd3228b0d5ee /posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html
parent30d15372300c4fc0e6e519b0fc528cba8b287fd9 (diff)
Publish deploy 2020-03-02 14:06
Diffstat (limited to 'posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html')
-rw-r--r--posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html19
1 files changed, 19 insertions, 0 deletions
diff --git a/posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html b/posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html
new file mode 100644
index 0000000..b2ba6be
--- /dev/null
+++ b/posts/2020-01-14-Converting-between-PIL-NumPy/index 5.html
@@ -0,0 +1,19 @@
+<!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><head><script src="https://www.googletagmanager.com/gtag/js?id=UA-108635191-1v"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'UA-108635191-1');</script></head><body class="item-page"><header><div class="wrapper"><a class="site-name" href="/">Navan Chauhan</a><nav><ul><li><a href="/about">About Me</a></li><li><a class="selected" href="/posts">Posts</a></li><li><a href="/publications">Publications</a></li><li><a href="/assets/résumé.pdf">Résumé</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><span class="reading-time">Created on January 14, 2020</span><span class="reading-time">Last modified on January 18, 2020</span><h1>Converting between image and NumPy array</h1><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">numpy</span>
+<span class="kn">import</span> <span class="nn">PIL</span>
+
+<span class="c1"># Convert PIL Image to NumPy array</span>
+<span class="n">img</span> <span class="o">=</span> <span class="n">PIL</span><span class="o">.</span><span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;foo.jpg&quot;</span><span class="p">)</span>
+<span class="n">arr</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
+
+<span class="c1"># Convert array to Image</span>
+<span class="n">img</span> <span class="o">=</span> <span class="n">PIL</span><span class="o">.</span><span class="n">Image</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
+</div>
+
+</code></pre><h2>Saving an Image</h2><pre><code><div class="highlight"><span></span><span class="k">try</span><span class="p">:</span>
+ <span class="n">img</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">destination</span><span class="p">,</span> <span class="s2">&quot;JPEG&quot;</span><span class="p">,</span> <span class="n">quality</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">optimize</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">progressive</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
+<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
+ <span class="n">PIL</span><span class="o">.</span><span class="n">ImageFile</span><span class="o">.</span><span class="n">MAXBLOCK</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
+ <span class="n">img</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">destination</span><span class="p">,</span> <span class="s2">&quot;JPEG&quot;</span><span class="p">,</span> <span class="n">quality</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">optimize</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">progressive</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
+</div>
+
+</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><div id="disqus_thread"></div><script src="/assets/disqus.js"></script><noscript>Please enable JavaScript to view the comments</noscript></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> \ No newline at end of file