检测图像文件是否损坏
作者:互联网
import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers import os num_skipped = 0 for folder_name in ("Cat", "Dog"): #os.path.join()连接两个或更多的路径名组件 folder_path = os.path.join(".\PetImages", folder_name) #os.listdir(path)列出该目录下的子目录 for fname in os.listdir(folder_path): fpath = os.path.join(folder_path, fname) try: #open() 函数用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。 fobj = open(fpath, mode="rb") is_jfif = tf.compat.as_bytes("JFIF") in fobj.peek(10) finally: fobj.close() if not is_jfif: num_skipped += 1 # Delete corrupted image os.remove(fpath) print("Deleted %d images" % num_skipped)
标签:检测,损坏,fobj,num,import,path,图像文件,os,folder 来源: https://www.cnblogs.com/hsy1941/p/15092694.html