对图像数据进行清洗过程中,为了判断图像文件是否可读,可以调用PIL(Pillow)包的 Image 类。可以使用 Image 类的 open 方法,该方法会尝试打开图像文件并返回一个 Image 对象。如果文件不可读,该方法将抛出一个 IOError 异常。具体代码如下
from PIL import Image# 使用Pillow检查图像文件是否可读
try:image = Image.open('example.jpg')# 如果文件可读,可以继续处理图像print('Image is readable by Pillow.')
except IOError:print('Image is not readable by Pillow.')