Parsing a graph image

Dan Stromberg drsalists at gmail.com
Fri May 13 19:23:08 EDT 2011


You could convert the image to a netpbm format, like ppm, pgm or pbm.  If
the image has few colors, that should be quite viable.  Each of these
formats are easy to work with in Python.  ppm is for color, pgm is for
grayscale, pbm is for strictly black and white, pnm pertains to any of the
3.  There are all sorts of filters for converting between these formats and
many others in netpbm, and image processing.

You might be able to use pnmcut (to select just the part of the image you
need - I'm guessing your math will be simpler if you crop off the axes and
everything outside them), ppmdist (to convert color (with a low number of
colors) to grayscale) or ppmtopgm (for high color images), and pgmedge (for
edge detection), etc.  Then you might pnmnoraw to get an ASCII pbm instead
of a binary one - ASCII netpbm files are even simpler than binary onesto
work with.

Here's an example of generating a ppm file in Python 2.  It looks like this
after conversion to png format:
http://stromberg.dnsalias.org/~dstromberg/software/circle.png
If you set a bunch of them next to each other (say, by tiling it as a
wallpaper), it gives a slightly eerie 3D effect.  It's an application of the
inverse square law.  I know, it's kind of the opposite of what you need, but
it may still illustrate the approach:

#!/usr/bin/python

# writes a ppm file for a sort of sphere on stdout.

import math
import sys

def pad(n):
        s = str(n)
        while len(s) < 4:
                s = s + ' '
        return s

size=200
colors=256
print 'P3'
print size,size
print colors-1
half=float(size/2)
sqrt2 = math.sqrt(2)
for i in range(0,size):
        for j in range(0,size):
                x = float(i - half) / half
                y = float(j - half) / half
                z = math.sqrt(x*x + y*y)/sqrt2
                col = int(z * (colors-1))
                #sys.stderr.write(str(x)+' '+str(y)+' '+str(col)+'\n')
                ch = chr(col)
                sys.stdout.write(pad(col)+pad(col)+pad(col)+' ')
        print

BTW, I don't do string concatenation this much anymore, and xrange is good
in 2.x.  :)

On Fri, May 13, 2011 at 3:19 AM, Bastian Ballmann <balle at chaostal.de> wrote:

> Hi python lovers out there,
>
> I am searching for a library to parse data from a graph in an image file
> something like http://pytseries.sourceforge.net/_images/yahoo.png
> Any suggestions, hints, links?
>
> TIA && have a nice day! :)
>
> Basti
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110513/87f4f296/attachment-0001.html>


More information about the Python-list mailing list