本文主要是介绍更换数据集导致错误OSError: Unrecognized data stream contents when reading image file,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 查找错误
- 打印错误文件并查看其二进制开头
- 移动文件
- 补充,将后缀是png但实际是jpg图像转换为png图像
查找错误
比如我的png格式图片要一样的开头时
def is_valid_image(path):try:bValid = Truefileobj = open(path, 'rb') # 以二进制打开文件buf = fileobj.read()if not buf.startswith(b'\x89PNG'): # 是否以\x89PNG开头 表示PNGbValid = Falseprint(buf)else:try:Image.open(fileobj).verify()except Exception as e:bValid = Falseexcept Exception as e:return Falsereturn bValid
jpg格式
def is_valid_image(path):try:bValid = Truefileobj = open(path, 'rb') # 以二进制打开文件buf = fileobj.read()if not buf.startswith(b'\xff\xd8'): # 是否以\xff\xd8开头 表示JPEG(jpg)bValid = Falseelse:try:Image.open(fileobj).verify()except Exception as e:bValid = Falseexcept Exception as e:return Falsereturn bValid
打印错误文件并查看其二进制开头
以png为例,如果不想看二进制开头可注释掉print(buf)
import os
from PIL import Image
import shutildef is_valid_image(path):try:bValid = Truefileobj = open(path, 'rb') # 以二进制打开文件buf = fileobj.read()if not buf.startswith(b'\x89PNG'): # 是否以\x89PNG开头 表示PNGbValid = Falseprint(buf)else:try:Image.open(fileobj).verify()except Exception as e:bValid = Falseexcept Exception as e:return Falsereturn bValiddef is_call_valid(path, move_to_path):# 遍历图像夹下所有图像 root:根目录 dirs:根目录下所有目录(文件夹):files: 包含所有图像的一个listfor root, dirs, files in os.walk(path):for img_file in files:# 组合图像的绝对路径img_file_path = os.path.join(root, img_file)# 调用图像判断函数flag = is_valid_image(img_file_path)# 判断图像是否损坏,若是则移动到失效文件路径中if flag == False:# this delete can not restore# os.remove(img_file_path)print(img_file_path)# 移动文件# shutil.move(img_file_path, move_to_path)# print(img_file_path)# is_call_valid('./dataset/train_data/ors-4199/gt','./fail ors-4199')
is_call_valid('./dataset/train_data/RSISOD/gt','./fail RSISOD')
移动文件
import os
from PIL import Image
import shutildef is_valid_image(path):try:bValid = Truefileobj = open(path, 'rb') # 以二进制打开文件buf = fileobj.read()if not buf.startswith(b'\x89PNG'): # 是否以\x89PNG开头 表示PNGbValid = Falseelse:try:Image.open(fileobj).verify()except Exception as e:bValid = Falseexcept Exception as e:return Falsereturn bValiddef is_call_valid(path, move_to_path):# 遍历图像夹下所有图像 root:根目录 dirs:根目录下所有目录(文件夹):files: 包含所有图像的一个listfor root, dirs, files in os.walk(path):for img_file in files:# 组合图像的绝对路径img_file_path = os.path.join(root, img_file)# 调用图像判断函数flag = is_valid_image(img_file_path)# 判断图像是否损坏,若是则移动到失效文件路径中if flag == False:# this delete can not restore# os.remove(img_file_path)# 移动文件shutil.move(img_file_path, move_to_path)# print(img_file_path)is_call_valid('./dataset/train_data/ors-4199/gt','./fail ors-4199')
is_call_valid('./dataset/train_data/RSISOD/gt','./fail RSISOD')
补充,将后缀是png但实际是jpg图像转换为png图像
from PIL import Image
import os# 设置输入文件夹路径和目标格式
input_folder = './dataset/train_data/RSISOD/gt/pool'
target_format = 'png'# 循环遍历输入文件夹中的所有文件
for filename in os.listdir(input_folder):# 检查文件是否为jpg格式fileobj = open(os.path.join(input_folder, filename), 'rb') # 以二进制打开文件buf = fileobj.read()if not buf.startswith(b'\x89PNG'):# 组合新的文件名和路径img_path_jpg = os.path.join(input_folder, filename)img_path_png = os.path.splitext(img_path_jpg)[0] +'!'+ '.' + target_format# 读取JPG格式图片并保存为PNG格式with Image.open(img_path_jpg) as img:img.save(img_path_png)# 删除原始JPG格式图片os.remove(img_path_jpg)
这篇关于更换数据集导致错误OSError: Unrecognized data stream contents when reading image file的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!