我的程序给出了一个坐标错误。我无法找出原因

时间:2016-03-24 14:03:52

标签: python

from cImage import *

im = FileImage("image.gif")

width = im.getWidth()

height = im.getHeight()

im2 = FileImage("background.gif")



a = height-1

x=-1

y=0

for row in range(height):

    for column in range(width):

        if x<=a:

            x=x+1

        else:

            y=y+1

            x=0

        p = im.getPixel(x,y)

        h = p[0]

        i = p[1]

        j = p[2]

        average = (h+i+j)/3

        if average < 235:

            im2.setPixel(x,y,Pixel(h,i,j))

im2.save("background.gif")

1 个答案:

答案 0 :(得分:1)

您无需手动管理xy。这就是for循环的用途。只需写下:

for y in range(height):
    for x in range(width):
        p = im.getPixel(x,y)
        ...

事实上,您手动处理这些问题会导致问题,因为您将x设置为0height - 1之间的范围,而不是width - 1