当前位置:主页 > 软件编程 > Python代码 >

Python 判断图像是否读取成功的方法

时间:2021-04-13 09:15:59 | 栏目:Python代码 | 点击:

大批量处理数据时,若因个别图像错误导致代码中断,从头再来比较浪费时间

对未成功读入的图像跳过(读图 import cv2)

for i in range(1,1000):
 image = cv2.imdecode(np.fromfile('xxx.jpg', dtype=np.uint8), -1)
 try:
  image.shape 
 except:
  print('fail to read xxx.jpg')
  continue
 ......

若该图像可能不存在,即没有该图像的文件名,也可用try判断

try:
 np.fromfile('xxx.jpg', dtype=np.uint8)
except:
 continue

您可能感兴趣的文章:

相关文章