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

yipeiwu_com7年前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

以上这篇Python 判断图像是否读取成功的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

关于ResNeXt网络的pytorch实现

此处需要pip install pretrainedmodels """ Finetuning Torchvision Models """ from __future__ im...

django中瀑布流写法实例代码

django中瀑布流写法实例代码

django中瀑布流初探 img.html <!DOCTYPE html> <html lang="en"> <head> <meta...

浅析python,PyCharm,Anaconda三者之间的关系

一、它们是什么? 1.python Python是一种跨平台的计算机程序设计语言,简单来说,python就是类似于C,Java,C++等,一种编程语言。 2.Anaconda Anaco...

python sys,os,time模块的使用(包括时间格式的各种转换)

sys模块 sys.argv: 实现从程序外部向程序传递参数。 位置参数argv[0]代表py文件本身,运行方法 python xx.py 参数1,参数2 。。 self = s...

python中报错"json.decoder.JSONDecodeError: Expecting value:"的解决

在学习python语言中用json库解析网络数据时,我遇到了两个编译错误:json.decoder.JSONDecodeError: Expecting property name en...