0

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Morpheus
  • 55
  • 2
  • 8
  • 1
    What doesn't work? What do you expect to happen? What happens instead? – Joooeey Nov 06 '18 at 23:56
  • Hello. Thank you for reading my question. In the csv file that is created and in the console i get the following 75387968,0,1557,0,-1,-1,-115147036,659,0,0,1836020326,1769300512,1852404844.........20 x 20 – Morpheus Nov 07 '18 at 00:23

2 Answers2

0

Don't reinvent the wheel, use Pillow to load the image, then convert it to a numpy array:

import numpy as np
from PIL import Image

im = np.array(Image.open('pgmDataSet/40157883_11_2.pgm'))

Now print im.shape and off you go...

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks so much. I tried it and I got this error message:: Traceback (most recent call last): File "C:/Users/Keith/PycharmProjects/CSC3060Ass01/Ass1ConvertCsv.py", line 4, in im = np.array(Image.open('pgmDataSet/40157883_11_1.pgm')) File "C:\Users\Keith\Anaconda3\envs\CSC3060Ass01\lib\site-packages\PIL\Image.py", line 2657, in open % (filename if filename else fp)) OSError: cannot identify image file 'pgmDataSet/40157883_11_1.pgm' Process finished with exit code 1 – Morpheus Nov 07 '18 at 00:52
  • I just checked PILLOW version and its 5.3.0 – Morpheus Nov 07 '18 at 01:01
  • Was recommended to install conda install -c conda-forge pillow – Morpheus Nov 07 '18 at 01:07
  • I can print(im) and get it working if i import a .jpeg instead. We were told use .pgm because it was easier. To save time I'm going to convert everything to .jpeg. So Mark Setchell thank you very much. I wish I asked a week ago – Morpheus Nov 07 '18 at 01:32
0

Your PGM file doesn't look correct to me. You show it as:

P2
# Created by GIMP version 2.10.6 PNM plug-in
20 20
2552552552552552552550255........255 or 0 black and white hand drawn

but that isn't a valid PGM/P2 file. In addition to specifying a width and height (which I assume are 20,20 here), you also need to give a maximum gray value (which appears to be 255).

Not only that, but according to http://netpbm.sourceforge.net/doc/pgm.html , each pixel value must have whitespace before and after it. The whitespace is not optional.

Therefore, your PGM file should look something like:

P2
# Created by ...
20 20 255
255 255 255 255 255 255 255 0 255 ........ 255

Until you fix this issue (of the badly-formatted PGM file), I doubt there'll be many libraries that will open and read your .pgm file.

So fix your .pgm issue, and see if that doesn't help you with the rest of your problem.

J-L
  • 1,786
  • 10
  • 13
  • I see what you mean, thanks. I converted my hand drawn images with GIMP when I open with notepad there are no spaces. I don't have time to look at it now, using jpeg worked but when I have finished I will come back to this as I wasted a lot of time on it so I want to know why it wouldn't work and identify where I went wrong. – Morpheus Nov 08 '18 at 15:00
  • Also, it looks like your PGM file only has black (0) and white (255) values. If this is the case, then you make things simpler on you by using a PBM file (P1). ( http://netpbm.sourceforge.net/doc/pbm.html ) This format uses 0 for white and 1 for black. – J-L Nov 08 '18 at 16:58