Skip to content Skip to sidebar Skip to footer

How Can I Convert A Png To A Dataframe For Python?

I trained a model for Digit Recognizer (https://www.kaggle.com/c/digit-recognizer/data). The input data is a csv file. Each row in the file represent an image which is 28 pixels in

Solution 1:

I prepared a little example for you, which gives you hopefully an idea on how you can achieve this task:

I am using this image as example:

example image

Full script:

import numpy as np
import cv2 
import csv 

img = cv2.imread('./1.png', 0) # load grayscale image. Shape (28,28)

flattened = img.flatten() # flatten the image, new shape (784,)

flattened = np.insert(flattened, 0, 0) # insert the label at the beginning of the array, in this case we add a 0 at the index 0. Shape (785,0)


#create column names 
column_names = []
column_names.append("label")
[column_names.append("pixel"+str(x)) for x in range(0, 784)] # shape (785,0)

# write to csv 
with open('custom_test.csv', 'w') as file:
    writer = csv.writer(file, delimiter=';')
    writer.writerows([column_names]) # dump names into csv
    writer.writerows([flattened]) # add image row 
    # optional: add addtional image rows

Now you have the same csv structure as provided in your example.

custom_test.csv output (shortened):

label;pixel0;pixel1;pixel2;pixel3;pixel4;pixel5;pixel6;pixel7;pixel ...
0;0;0;0;0;0;0;0;0;0;0;0....

EDIT: To visualize the flattened image with matplotlib, you have to specfiy a colormap:

row = flattened.reshape(28,28)
plt.imshow(row, cmap='gray') # inverse grayscale is possible with: cmap='gray_r'

Post a Comment for "How Can I Convert A Png To A Dataframe For Python?"