这是整个模块。所有的" elif"语句是有效的语法,如果改为"如果"语句
def processRGB(item, matrix):
threshhold = 229
red, green, blue = matrix[0, 0]
if red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 0)
red, green, blue = matrix[item[0].size[0] - 1, 0]
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 90)
red, green, blue = matrix[0, item[0].size[1] - 1]
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 180)
red, green, blue = matrix[item[0].size[0] - 1, item[0].size[1] - 1]
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 270)
final = picture[0].crop(boundary)
final.load()
return final
答案 0 :(得分:1)
在python中,if
语句会在缩进更改后立即结束。如果没有前一个elif
,你就不能拥有if
。我猜你希望你的代码看起来像这样,因为缩进的不同,这是有效的语法。
def processRGB(item, matrix):
threshhold = 229
red, green, blue = matrix[0, 0]
if red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 0)
red, green, blue = matrix[item[0].size[0] - 1, 0]
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 90)
red, green, blue = matrix[0, item[0].size[1] - 1]
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 180)
red, green, blue = matrix[item[0].size[0] - 1, item[0].size[1] - 1]
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 270)
final = picture[0].crop(boundary)
final.load()
return final
答案 1 :(得分:0)
如果前一个elif
语句为真,那么if
行之前的语句应该缩进到与它们之前的行相同的级别。否则,elif
应该是if
,因为您正在访问另一个代码块。
答案 2 :(得分:0)
这是因为:
if red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 0)
上面的内容如果在此结束,因为范围是由缩进定义的。
red, green, blue = matrix[item[0].size[0] - 1, 0]
因为“if”和“elif”语句之间有一些代码。 “elif”与上述“if”无关,我们不能将“elif”指定为独立声明。它应该与“if”或其他“elif”语句相关联,这不是在这种情况下。
elif red <= threshhold and green <= threshhold and blue <= threshhold:
picture, boundary = RGBUpright(item, matrix, 90)
与其他“elif”相同的解释。
如果不清楚,请告诉我。