Question

I'm writing a program for a college course. I import a .PPM file saved as a 2-d array from main into the function. Then I have to update the pixels of a graphics window (which is opened in main) using .setPixel and color_RGB() method and functions.

The pixels are updating, however there is a white pixel in between each colored pixel for some reason. It's not the PPM file (they were supplied by my professor and I've tried multiple ones), so it has to be my function.

Warning: I am not allowed to use anything in my program that we have not yet covered in our course (it's a first year, 4 month course so the scope is not massive). I don't need to know exactly HOW to fix it, as much as I need to know why it's doing it (AKA: I need to be able to explain how I fixed it, and why it was breaking in the first place).

Here is my function:

def Draw_Pic(pic,pic_array, sizeX, sizeY, gfx_window):

for y in range(sizeY):

    for x in range(0, sizeX, 3):

        pixel_color = color_rgb(pic_array[y][x],pic_array[y][x+1],pic_array[y][x+2])


        pic.setPixel(x,y,pixel_color)
        gfx_window.update()
Was it helpful?

Solution

You are using range(0, sizeX, 3) which creates a list with values 0 to sizeX with increment 3.

So your x goes 0..3..6..9 etc. Makes perfect sense for the part where you assemble pixel color from 3 components, but then you do pic.setPixel(x,y,colors) using the same interleaved x.

Hope that helped.

P.S. By the way, why "colors" and not "color"?

edit Also, that way you'll copy only 1/3 of the image in pic_array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top