Convert .csv file to Images. A simple solution to change pixel…
By: Sanchit Gupta
Tags: Programming, Image Processing, Projects, Neural Networks, Python

Websites like Kaggle provide these datasets and often have on-going challenges to judge the accuracy of the results of a model trained by one of the many enthusiasts. These datasets, because of their size are usually provided as .csv files (usually read by Microsoft Excel) and can be downloaded for free from these websites.

Now you might think, shouldn’t the training data of a task like Image Classification be made of images rather than an excel sheet?

To understand and answer this question, we first need to understand what an image that we see on a screen (digital image) is made up of. Any digital image is composed of small discrete elements called pixels, the numeric value of these pixels represent the intensity value of a particular channel at the location of the pixel. A 2-dimensional array of such pixel values is what makes a digital image. A grayscale image usually has a single 2D channel, while a colored image has 3 channels for red, green, and blue.

Hence, to create an image all we need is the pixel’s numeric intensity values and the locations in a particular channel. The datasets we get from different sources in a .csv file give us exactly that.

As a beginner, you always feel better when you work with images that you can see and not be daunted with several thousand lines of numbers in an excel sheet. I have put together a small code snippet to convert the data from an excel sheet to images, which I will explain and then leave a link for anyone to use and download.

Converting .csv file to images

We start by importing a few basic libraries through which we can use functions that help us minimize the code we write.

import numpy as np

import pandas as pd

import cv2 import utils

import os

Make sure you have all these libraries installed and as a good practice, you can run the code with a print statement after all the imports to make sure there is no issue. Make use of pip if you need to install any of these libraries.

Next, you want to read the .csv file into python and assign the data to a variable. The easiest way to accomplish this is to use pandas. This library has many useful applications in data analysis and data manipulation and makes handling large datasets easy.

# data

data = pd.read_csv('test.csv') # path of the .csv file #print(data.shape) # to check the shape

#print(data.head(5)) # print the first 5 lines of the data

The read_csv(“path”) function helps to load the data and assign it to the variable. This data is now converted from a .csv file to a multi-dimensional matrix. A good practice is to check the shape of this matrix, this helps to structure the process in your head. The data.shape prints the shape of the data on the console. Another very helpful function is data.head(“number of values”), this helps to read the first ’n’ rows of the data and can provide an insight into the values read by python.

Now we move on to the heart of this code, which is the function that takes in a single row of the .csv file data and returns an image made using pixel values we got from the .csv file.

def convert2image(row):

pixels = row['pixels'] # In dataset,row heading was 'pixels' img = np.array(pixels.split())

img = img.reshape(48,48) # dimensions of the image

image = np.zeros((48,48,3)) # empty matrix

image[:,:,0] = img

image[:,:,1] = img

image[:,:,2] = img

return image.astype(np.uint8) # return the image

The convert2image(row) function takes the row of excel sheet values as input and assigns them to pixels. In the dataset, the heading of the column we want to separate is ‘pixels’ which we use to select that column from the row we gave as an input to the function.

The pixels values are then reshaped and made into a 2-dimensional matrix using the NumPy library. Make sure you understand the np.reshape() function as not doing this correctly can change the image and make it unrecognizable.

You may notice in the code above that we also assign the same channel to 3 different channels (RGB) in an image and then return. We do this because we don’t want to see only the grayscale version of the image. Once we have the image in the right shape and the right channels, we can return it from the function.

Now that we have the heart of our code all that is left is to take out a single row and call the function with that row being the input.

count = 0 # initialize counter for i in range(1, data.shape[0]): #data.shape[0] gives no. of rows

face = data.iloc[i] # remove one row from the data

img = convert2image(face) # send this row of to the function

# cv2.imshow("image", img)

# cv2.waitKey(0) # closes the image window when you press a key count+=1 # counter to save the images with different name

cv2.imwrite(r'path/'+ str(count) +'.jpg',img) # path of location

To save all the data in the .csv file to images, we use a ‘for loop’ which runs till we reach the last row of the data. The iloc[‘number’] from the pandas library provides an easy way to remove a particular row from the data file and send it to the function.

We know that we get an image back from the convert2image function that we wrote before. This image we can view using OpenCV function, cv2.imshow(‘window_name’, image). The waitKey(), allows you to specify the time before OpenCV kills the image window and moves on with the rest of the code. If you put 0 in the waitKey, then the program will wait to get a keyboard input before closing the image window.

The counter helps us to make sure that all the images we are saving have a unique name. The cv2.imwrite() function helps us to specify the path with the name of the image. Make sure to provide a unique name to each image, otherwise this function will overwrite all the images and you will end up with the last image after processing through the whole dataset.

That’s it!

Once you have all this code with the correct path and installed libraries, this code will give you images from the .csv file. The complete code can be downloaded from here.

I am happy to tell you guys that I received an overwhelming response on my last blog about creating executable files using python, you can read it here. Let me know if there are any more specific code help you need me to cover. Till then, keep reading and supporting. Cheers!