Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write PIL image filter for plain pgm format?
    primarykey
    data
    text
    <p>How can I write a filter for python imaging library for pgm plain ascii format (P2). Problem here is that basic PIL filter assumes constant number of bytes per pixel. </p> <p>My goal is to open feep.pgm with Image.open(). See <a href="http://netpbm.sourceforge.net/doc/pgm.html" rel="nofollow">http://netpbm.sourceforge.net/doc/pgm.html</a> or below.</p> <p>Alternative solution is that I find other well documented ascii grayscale format that is supported by PIL and all major graphics programs. Any suggestions?</p> <p>feep.pgm:</p> <pre><code>P2 # feep.pgm 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 </code></pre> <p><strong>edit:</strong> Thanks for the answer, It works... <strong>but</strong> I need a solution that uses Image.open(). Most of python programs out there use PIL for graphics manipulation (google: python image open). Thus, I need to be able to register a filter to PIL. Then, I can use any software that uses PIL. I now think mostly scipy, pylab, etc. dependent programs.</p> <p><strong>edit</strong> Ok, I think I got it now. Below is the wrapper pgm2pil.py:</p> <pre><code>import Image import numpy def pgm2pil(filename): try: inFile = open(filename) header = None size = None maxGray = None data = [] for line in inFile: stripped = line.strip() if stripped[0] == '#': continue elif header == None: if stripped != 'P2': return None header = stripped elif size == None: size = map(int, stripped.split()) elif maxGray == None: maxGray = int(stripped) else: for item in stripped.split(): data.append(int(item.strip())) data = numpy.reshape(data, (size[1],size[0]))/float(maxGray)*255 return numpy.flipud(data) except: pass return None def imageOpenWrapper(fname): pgm = pgm2pil(fname) if pgm is not None: return Image.fromarray(pgm) return origImageOpen(fname) origImageOpen = Image.open Image.open = imageOpenWrapper </code></pre> <p>There is a slight upgrade to misha's answer. Image.open has to be saved in order to prevent never ending loops. If pgm2pil returns None wrapper calls pgm2pil which returns None which calls pgm2pil...</p> <p>Below is the test function (feep_false.pgm is a malformed pgm e.g. "P2" -> "FOO" and lena.pgm is just <strong>the</strong> image file):</p> <pre><code>import pgm2pil import pylab try: pylab.imread('feep_false.pgm') except IOError: pass else: raise ValueError("feep_false should fail") pylab.subplot(2,1,1) a = pylab.imread('feep.pgm') pylab.imshow(a) pylab.subplot(2,1,2) b = pylab.imread('lena.png') pylab.imshow(b) pylab.show() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload