Can't get fractal image to work
Question: So I'm working on a homework assignment regarding using image objects in
python. I'm using python 3.4.1 for this assignment. I feel like I have
everything done, but it doesn't want to work correctly. Basically, I'm trying
to get it to look like the picture that I've attached, but it only shows as 1
red line across, and 1 red line top to bottom on a white background. Any help
would be much appreciated.

The attached image: <http://imgur.com/TMho41w>

    
    
    import cImage as image
    
    width = 500
    height = 500
    
    img = image.EmptyImage(width, height)
    win = image.ImageWin("Exercise 3", width, height)
    img.draw(win)
    
    for row in range(height):
        for col in range(width):
            p = img.getPixel(col, row)
    
            if row == 0 or col == 0:
                p = image.Pixel(255, 0, 0)
            else:
                Sum = 0
                temppixel = img.getPixel(col-1, row)
                if temppixel.getRed() == 255:
                    Sum = Sum + 1
                elif temppixel.getBlue() == 255:
                    Sum = Sum + 2      
    
                temppixel = img.getPixel(col-1, row-1)
                if temppixel.getRed() == 255:
                    Sum = Sum + 1
                elif temppixel.getBlue() == 255:
                    Sum = Sum + 2  
    
                temppixel = img.getPixel(col, row-1)
                if temppixel.getRed() == 255:
                    Sum = Sum + 1
                elif temppixel.getBlue() == 255:
                    Sum = Sum + 2                
    
                if Sum % 3 == 1:
                    p = image.Pixel(255, 0, 0)
                elif Sum % 3 == 2:
                    p = image.Pixel(0, 0, 255)
                else:
                    p = image.Pixel(255, 255, 255)
    
            img.setPixel(col, row, p)
    
        img.draw(win)        
    
    img.draw(win)
    
    # uncomment this to save the image as a file
    #img.saveTk("gradient.gif")
    
    win.exitonclick()
    


Answer: Unfortunately, your code does exactly what you have written it to do. Let's
name the three first `if ... elif` condition1, 2 and 3 :

  1. The first pixel is set to red
  2. Then we progress through the first line, so `row = 0` which means condition 2 and 3 are using invalid coordinates (because of `row-1`). So there's only condition at play here, and it will always increment by 1 `sum` which means it'll add a new red pixel. 
  3. So you have now your first red line.
  4. For the first column, starting from the second line : conditions 1 & 2 are using invalid coordinates. Condition 3 will always return `sum = 1` which means a new red pixel. And you have your red line from top to bottom
  5. And then from `row = 1` and `col = 1`, all neighbors are red, which leads to a new white pixel. Unfortunately, white does contain some red, so it'll always be the sames conditions that are met, and you have your white background.

I haven't been able to find the complete algorithm for this method to build a
Sierpinski carpet, so I can't really correct it. But you should be extra
careful with these edges situations : what should be the three neighbors if
you are on the first line or first row ?

