Python 如何使用if函数作为try-except

Python 如何使用if函数作为try-except,python,python-imaging-library,Python,Python Imaging Library,我有一个程序,里面有一个循环。 在这个循环中,我从一个文件加载图像。 但也有一些不可用的图像,我不想手工排序。 我有以下代码: img = Image.open("downloads/parrot/" + pictures[i]) img = img.resize((150,150)) img.save("Validation/parrot/" + "picture" + str(i) + ".png") i = i + 1 我尝

我有一个程序,里面有一个循环。 在这个循环中,我从一个文件加载图像。 但也有一些不可用的图像,我不想手工排序。 我有以下代码:

        img = Image.open("downloads/parrot/" + pictures[i])
        img = img.resize((150,150))
        img.save("Validation/parrot/" + "picture" + str(i) + ".png")
        i = i + 1
我尝试使用try-except方法,但它总是停止程序。 有没有任何方法可以使用if循环,比如“if-imageisusivery()=false:”? 或者你还有其他想法吗?
谢谢你的帮助。

如果你不想让你的
尝试/除了
停止你的代码,你可以
通过

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

根据错误发生的位置(打开或调整大小),您可能需要移动对象:

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]))

你所说的“无用图像”是什么意思?如果你添加更多的上下文,也许我们可以帮助你。谢谢你,这很有帮助