PIL图像联系表代码if语句语法错误排查求助
解决PIL联系表代码中的语法错误
你的语法错误根源是paste方法的参数括号未闭合,并非if语句本身的问题——Python解析器因前面的括号不匹配,报错位置指向了后续的if行。
修正后的代码
# facecrops is a list of the PIL image objects print(face_crops) # create contact sheet to paste images to contact_sheet=Image.new('RGB', (500,300)) # add each resized cropped image to the contact sheet # use one variable for the X dimension, and one for the Y dimension. x=0 y=0 for pic in face_crops: # first resize the cropped face smallpic = size_to_fit(pic) # Lets paste the current image into the contact sheet contact_sheet.paste(smallpic, (x, y, smallpic.size[0], smallpic.size[1])) # 补全闭合括号 # update our position. If it is going to be greater than the width of the image, then set it to 0 # and update Y as well to point to the next "line" of the contact sheet. if (x + smallpic.width) >= contact_sheet.width: x=0 y=y+smallpic.height else: x=x+smallpic.width display(contact_sheet)
修改说明
- 补全
contact_sheet.paste行末尾的闭合括号:原代码中这一行的参数元组只写了开头的(,未写结尾的),导致语法结构断裂,Python无法正确解析后续代码,进而误报if语句行存在语法错误。 - 代码逻辑无需调整,仅修复括号问题即可解决报错。
内容的提问来源于stack exchange,提问作者ajeking
相关产品推荐
相关产品推荐

