Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV to image in python

Tags:

python

csv

image

I want to create an image out of an csv data.

I am reading the csv with:

f = open('file.csv', 'rb')
reader = csv.reader(f)

From here, I want to make a grayscale image that is translating each row of numbers in the list into a line of intensities in an image file.

Not sure what would be useful but here are some details about my csv file: using floats, columns:315, rows: 144

Thanks

like image 657
Voltronika Avatar asked Oct 27 '25 23:10

Voltronika


2 Answers

Two steps:

  1. convert the csv file to a numpy array using genfromtxt

From @Andrew on How to read csv into record array in numpy?

from numpy import genfromtxt
my_data = genfromtxt('my_file.csv', delimiter=',')
  1. then save the numpy array as an image
like image 85
Vincent Avatar answered Oct 30 '25 15:10

Vincent


from numpy import genfromtxt
from matplotlib import pyplot
from matplotlib.image import imread
my_data = genfromtxt('path to csv', delimiter=',')
matplotlib.image.imsave('path to save image as Ex: output.png', my_data, cmap='gray')
image_1 = imread('path to read image as Ex: output.png')
# plot raw pixel data
pyplot.imshow(image_1)
# show the figure
pyplot.show()
like image 38
Vignesh Avatar answered Oct 30 '25 17:10

Vignesh