Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Convert Image (JPG, PNG) to CSV

#1
Python Convert Image (JPG, PNG) to CSV

5/5 – (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:

  1. Read the image into a PIL.Image object.
  2. Convert the PIL.Image object to a 3D NumPy array with the dimensions rows, columns, and RGB values.
  3. Convert the 3D NumPy array to a 2D list of lists by collapsing the RGB values into a single value (e.g., a string representation).
  4. Write the 2D list of lists to a CSV using normal file I/O in Python.

Here’s the code that applies these four steps, assuming the image is stored in a file named 'c++.jpg':

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')

Note that the resulting CSV file looks like this with super long rows.


Each CSV cell (column) value is a representation of the RGB value at that specific pixel. For example, [255 255 255] represents the color white at that pixel.


For more information and some background on file I/O, check out our detailed tutorial on converting a list of lists to a CSV:

? Related Tutorial: How to Convert a List to a CSV File in Python [5 Ways]



https://www.sickgaming.net/blog/2022/09/...ng-to-csv/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,389 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] I Created My First DALL·E Image in Python OpenAI Using Four Easy Steps xSicKxBot 0 1,271 03-10-2023, 03:46 PM
Last Post: xSicKxBot
  [Tut] Image to PDF Converter and PDF Merger | Python xSicKxBot 0 1,151 01-12-2023, 03:26 PM
Last Post: xSicKxBot
  [Tut] Creating an Advent Calendar App in Python with AI Image Creation xSicKxBot 0 1,217 12-09-2022, 06:12 AM
Last Post: xSicKxBot
  [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? xSicKxBot 0 1,329 12-08-2022, 01:23 PM
Last Post: xSicKxBot
  [Tut] How to Convert Octal String to Integer in Python xSicKxBot 0 1,292 12-04-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] Python Convert Hex to Base64 xSicKxBot 0 1,275 11-30-2022, 09:32 PM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,301 10-04-2022, 11:37 AM
Last Post: xSicKxBot
  [Tut] Python Convert Parquet to CSV xSicKxBot 0 1,183 09-02-2022, 03:20 PM
Last Post: xSicKxBot
  [Tut] Python Convert Markdown Table to CSV xSicKxBot 0 1,220 09-01-2022, 01:21 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016