[英]How to use a if function as try except
I have a program which has a loop in it.我有一个程序,里面有一个循环。 In this loop I load images from a file.
在这个循环中,我从文件加载图像。 But there are some not usable images, that I don`t want to sort by hand.
但是有一些不可用的图像,我不想手动排序。 I have this code:
我有这个代码:
img = Image.open("downloads/parrot/" + pictures[i])
img = img.resize((150,150))
img.save("Validation/parrot/" + "picture" + str(i) + ".png")
i = i + 1
I tried to use the try except method, but it always stopped the program.我尝试使用 try except 方法,但它总是停止程序。 Is there any way to use an if loop, like "if imageisuseful() = false:"?
有没有办法使用 if 循环,比如“if imageisuseful() = false:”? Or do you may have any other Ideas?
或者你可能有任何其他的想法? Thank you for your help.
感谢您的帮助。
If you don't want your try / except
to stop your code, you can pass
:如果你不希望你的
try / except
停止你的代码,你可以pass
:
try:
img = Image.open("downloads/parrot/" + pictures[i])
img = img.resize((150,150))
img.save("Validation/parrot/" + "picture" + str(i) + ".png")
i = i + 1
except:
pass # pictures[i] raised an exception here
Depending on where the error is happening (either at the open or the resize) you might need to move things around:根据错误发生的位置(在打开或调整大小时),您可能需要移动一些东西:
img = Image.open("downloads/parrot/" + pictures[i])
try:
img = img.resize((150,150))
img.save("Validation/parrot/" + "picture" + str(i) + ".png")
i = i + 1
except:
print("FILE {} DOESN'T WORK".format(pictures[i]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.