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")
答案 0 :(得分:1)
您无需手动管理x
和y
。这就是for循环的用途。只需写下:
for y in range(height):
for x in range(width):
p = im.getPixel(x,y)
...
事实上,您手动处理这些问题会导致问题,因为您将x
设置为0
和height - 1
之间的范围,而不是width - 1
。