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!

Homework Help: 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")
    filename = input("name of image file: ")

    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)


    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)

    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?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook