1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

PPM image editor Python

  1. Apr 26, 2012 #1
    1. The problem statement, all variables and given/known data
    I'm writing a program that will do simple image editing with ppm files. I can only use the graphics library and things in there. No PIL or Image module.

    My problem is that I get an error that says "couldn't recognize data in image file "sole.ppm"
    I know for a fact it is a correct ppm file.

    3. The attempt at a solution

    Here's my code:

    Code (Text):
    def main():
    print("Image Editor")
    print()
    filename = input("name of image file: ")
    print()

    with open(filename) as f:
        formatind = f.readline()
        width, height = [int(x) for x in f.readline().split()]
        colordepth = f.readline()
        array = []
        for line in f:
            array.append([int(x) for x in line.split()])

    win = GraphWin("Image Editor!", width, height)

    image = Image(Point(100,100), filename)

    Display(image, array, width, height, win)

    inf.close()
    win.getMouse()
    win.close()

    main()
    And my display function looks like this:

    Code (Text):
    def Display(image, array, width, height, win):

    for i in range(width):
        for j in range(0, height, 3):
            colors = color_rgb(array[i][j], array[i][j+1], array[i][j+2])
            image.setPixel(i, j, colors)
            image.draw(win)

    return
    I just cannot figure out what's wrong with this. Any help would be great. Thanks
     
  2. jcsd
  3. Apr 26, 2012 #2
    I've just figured out that it's because of the format indicator. My file is P3. The Image constructor I used is not compatible with P3. I changed my file to P6 and it recognized the data. So I need another way of declaring an Image object for P3 format ppm file. Any ideas there?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: PPM image editor Python
  1. Equation editor (Replies: 1)

  2. Python syntax (Replies: 5)

  3. Python Beginner (Replies: 3)

  4. Python question (Replies: 1)

Loading...