python 删除损坏的图片数据

在图片数据处理的过程中遇到这样一个问题:
我想将图片中纯在不能打开损坏的图片删除掉(删除的文件包含字节数为空的图片和那些有字节数但是打不开的图片)
我的解决如下:
1: 首先判断图片数据字节是否为null;
2: 通过python imghdr 模块中what()函数获取有字节数但是不能打开图片的返回值是什么。
辨别是否所有打不开的图片返回的同一个类型,若是执行代码如下:
在这里插入图片描述

coding=utf-8

import imghdr
import os
def delect_webp_and_none_type(path):
for root,dir,file in os.walk(path):
for name in file:
target = (os.path.join(root,name))
result_type = imghdr.what(target)
if result_type == “webp” or result_type == None:
print(target)
os.remove(target)

if name ==“main”:
delect_webp_and_none_type(“F://picture/data/”)**

发布了25 篇原创文章 · 获赞 27 · 访问量 2200

猜你喜欢

转载自blog.csdn.net/m0_38053092/article/details/89102153