Sick Gaming
[Tut] Python Convert Image (JPG, PNG) to CSV - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python Convert Image (JPG, PNG) to CSV (/thread-99929.html)



[Tut] Python Convert Image (JPG, PNG) to CSV - xSicKxBot - 09-10-2022

Python Convert Image (JPG, PNG) to CSV

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;657549&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<p class="has-global-color-8-background-color has-background">Given an image as a <code>.png</code> or <code>.jpeg</code> file. How to convert it to a CSV file in Python?</p>
<p>Example image:</p>
<figure class="wp-block-image size-full"><img loading="lazy" width="771" height="771" src="https://blog.finxter.com/wp-content/uploads/2022/09/c.jpg" alt="" class="wp-image-657566" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/c.jpg 771w, https://blog.finxter.com/wp-content/uploads/2022/09/c-300x300.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/09/c-150x150.jpg 150w, https://blog.finxter.com/wp-content/uploads/2022/09/c-768x768.jpg 768w" sizes="(max-width: 771px) 100vw, 771px" /></figure>
<p>Convert the image to a CSV using the following steps:</p>
<ol class="has-global-color-8-background-color has-background">
<li>Read the image into a <code>PIL.Image</code> object.</li>
<li>Convert the <code>PIL.Image</code> object to a 3D NumPy array with the dimensions rows, columns, and RGB values.</li>
<li>Convert the 3D NumPy array to a 2D <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">list of lists</a> by collapsing the RGB values into a single value (e.g., a string representation).</li>
<li>Write the 2D <a href="https://blog.finxter.com/how-to-convert-a-list-of-objects-to-a-csv-file-in-python-5-ways/" data-type="post" data-id="510694" target="_blank" rel="noreferrer noopener">list of lists to a CSV</a> using normal file I/O in Python.</li>
</ol>
<p>Here’s the code that applies these four steps, assuming the image is stored in a file named <code>'c++.jpg'</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
from PIL import Image
import numpy as np # 1. Read image
img = Image.open('c++.jpg') # 2. Convert image to NumPy array
arr = np.asarray(img)
print(arr.shape)
# (771, 771, 3) # 3. Convert 3D array to 2D list of lists
lst = []
for row in arr: tmp = [] for col in row: tmp.append(str(col)) lst.append(tmp) # 4. Save list of lists to CSV
with open('my_file.csv', 'w') as f: for row in lst: f.write(','.join(row) + '\n')
</pre>
<p>Note that the resulting CSV file looks like this with super long rows.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="788" height="497" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-4.png" alt="" class="wp-image-657670" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-4.png 788w, https://blog.finxter.com/wp-content/uploads/2022/09/image-4-300x189.png 300w, https://blog.finxter.com/wp-content/uploads/2022/09/image-4-768x484.png 768w" sizes="(max-width: 788px) 100vw, 788px" /></figure>
</div>
<p>Each CSV cell (column) value is a representation of the RGB value at that specific pixel. For example, <code>[255 255 255]</code> represents the color white at that pixel.</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<p>For more information and some background on file I/O, check out our detailed tutorial on converting a list of lists to a CSV:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Tutorial</strong>: <a href="https://blog.finxter.com/how-to-convert-a-list-of-lists-to-a-csv-file-in-python/" data-type="post" data-id="7999" target="_blank" rel="noreferrer noopener">How to Convert a List to a CSV File in Python [5 Ways]</a></p>
</div>


https://www.sickgaming.net/blog/2022/09/08/python-convert-image-jpg-png-to-csv/