1

I have some jpg images I want to use for computer vision. How do I convert them to csv files? I tried an online converter but I have no idea if that actually works.

tes22
  • 21
  • 1
  • 1
  • 5
  • 2
    What kind of data would the csv file contain? For example, what would [this image of jelly beans](https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-256px-SIPI_Jelly_Beans_4.1.07.tiff.jpg) get converted into? – Kevin May 03 '19 at 14:11
  • 1
    @Kevin A text file which contains a description of each jelly bean, it's size and colour, but separated by a comma. Easy. Next? – Alan Kavanagh May 03 '19 at 14:13

2 Answers2

6

First you need to read the Image file. You can do it in various ways. I'll use PIL:

from PIL import Image
img = np.array(Image.open("image.jpg"))

Now to convert numpy array to csv file.

import csv

def csvWriter(fil_name, nparray):
  example = nparray.tolist()
  with open(fil_name+'.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerows(example)

csvWriter("myfilename", img)

In order to convert the csv file back to numpy array

import ast

def csvReader(fil_name):
  with open(fil_name+'.csv', 'r') as f:
    reader = csv.reader(f)
    examples = list(reader)
    examples = np.array(examples)
  t1=[]
  for x in examples:
    t2=[]
    for y in x:
      z= ast.literal_eval(y)
      t2.append(np.array(z))
    t1.append(np.array(t2))
  ex = np.array(t1)
  return ex

backToNumpyArray = csvReader("myfilename")
PaxPrz
  • 1,778
  • 1
  • 13
  • 29
2

Try using imageio.imread to convert it to a numpy array, and then convert it from there.

import numpy as np
import imageio

img = (imageio.imread(img_location, as_gray=True))/255
img = np.array(img)

np.savetxt("image.csv", a, delimiter=",")

And if you like pandas:

import pandas as pd
pd.DataFrame(img).to_csv("image.csv")

EDIT: This is assuming you'd want it grayscale. If you'd like in RGB, you could separate the channels into 3 2D arrays and create three separate files for each channel.

Andrew Bowling
  • 206
  • 2
  • 8