I need to find out how to convert .pgm file to .csv file in python. I have been learning python for 2 1/2 weeks so if anyone helps could you please keep my lack of knowledge in mind. I am using Pycharm on Windows 10.
Here is my code that I'm stuck with. I'm trying to open a pgm file and save it to an empty array print it out to check it has correct values then save it as a csv file with the intention of using it on a neural network. Here it the contents of the file.
P2
# Created by GIMP version 2.10.6 PNM plug-in
20 20
2552552552552552552550255........255 or 0 black and white hand drawn
import numpy
with open('pgmDataSet/40157883_11_2.pgm', 'r') as input_pgm:
list_test = [input_pgm]
list_test = numpy.empty([20, 20], dtype=int, order='C')
for row in list_test:
for col in row:
#What am i missing???????????
print(row)
numpy.savetxt('csvDataSet/40157883_11_2.csv', list_test, delimiter=',',
fmt='%s')
with open('csvDataSet/40157883_11_2.csv') as converted:
list_converted = [converted]
for line1 in list_converted:
print(line1.read())
# np.savetxt('csvDataSet/40157883_11_2.csv', input_pgm,
delimiter=',', fmt='%s')
# break;
# list_test[list_test >= 128] = 1
# list_test[list_test < 128] = 0
Don't reinvent the wheel, use Pillow to load the image, then convert it to a
numpy
array:Now print
im.shape
and off you go...