{"id":127931,"date":"2022-09-08T15:23:42","date_gmt":"2022-09-08T15:23:42","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=657549"},"modified":"2022-09-08T15:23:42","modified_gmt":"2022-09-08T15:23:42","slug":"python-convert-image-jpg-png-to-csv","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/08\/python-convert-image-jpg-png-to-csv\/","title":{"rendered":"Python Convert Image (JPG, PNG) to CSV"},"content":{"rendered":"\n<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<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>\n<p>Example image:<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/figure>\n<p>Convert the image to a CSV using the following steps:<\/p>\n<ol class=\"has-global-color-8-background-color has-background\">\n<li>Read the image into a <code>PIL.Image<\/code> object.<\/li>\n<li>Convert the <code>PIL.Image<\/code> object to a 3D NumPy array with the dimensions rows, columns, and RGB values.<\/li>\n<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>\n<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>\n<\/ol>\n<p>Here&#8217;s the code that applies these four steps, assuming the image is stored in a file named <code>'c++.jpg'<\/code>:<\/p>\n<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=\"\">\nfrom PIL import Image\nimport numpy as np # 1. Read image\nimg = Image.open('c++.jpg') # 2. Convert image to NumPy array\narr = np.asarray(img)\nprint(arr.shape)\n# (771, 771, 3) # 3. Convert 3D array to 2D list of lists\nlst = []\nfor row in arr: tmp = [] for col in row: tmp.append(str(col)) lst.append(tmp) # 4. Save list of lists to CSV\nwith open('my_file.csv', 'w') as f: for row in lst: f.write(','.join(row) + '\\n')\n<\/pre>\n<p>Note that the resulting CSV file looks like this with super long rows.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" 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=\"auto, (max-width: 788px) 100vw, 788px\" \/><\/figure>\n<\/div>\n<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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Given an image as a .png or .jpeg file. How to convert it to a CSV file in Python? Example image: Convert the image to a CSV using the following steps: Read the image into a PIL.Image object. Convert the PIL.Image object to a 3D NumPy array with the dimensions rows, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-127931","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127931"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127931\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}