比如我的png格式图片要一样的开头时
def is_valid_image(path):
try:
bValid = True
fileobj = open(path, 'rb') # 以二进制打开文件
buf = fileobj.read()
if not buf.startswith(b'\x89PNG'): # 是否以\x89PNG开头 表示PNG
bValid = False
print(buf)
else:
try:
Image.open(fileobj).verify()
except Exception as e:
bValid = False
except Exception as e:
return False
return bValid
jpg格式
def is_valid_image(path):
try:
bValid = True
fileobj = open(path, 'rb') # 以二进制打开文件
buf = fileobj.read()
if not buf.startswith(b'\xff\xd8'): # 是否以\xff\xd8开头 表示JPEG(jpg)
bValid = False
else:
try:
Image.open(fileobj).verify()
except Exception as e:
bValid = False
except Exception as e:
return False
return bValid
以png为例,如果不想看二进制开头可注释掉print(buf)
import os
from PIL import Image
import shutil
def is_valid_image(path):
try:
bValid = True
fileobj = open(path, 'rb') # 以二进制打开文件
buf = fileobj.read()
if not buf.startswith(b'\x89PNG'): # 是否以\x89PNG开头 表示PNG
bValid = False
print(buf)
else:
try:
Image.open(fileobj).verify()
except Exception as e:
bValid = False
except Exception as e:
return False
return bValid
def is_call_valid(path, move_to_path):
# 遍历图像夹下所有图像 root:根目录 dirs:根目录下所有目录(文件夹):files: 包含所有图像的一个list
for 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 shutil
def is_valid_image(path):
try:
bValid = True
fileobj = open(path, 'rb') # 以二进制打开文件
buf = fileobj.read()
if not buf.startswith(b'\x89PNG'): # 是否以\x89PNG开头 表示PNG
bValid = False
else:
try:
Image.open(fileobj).verify()
except Exception as e:
bValid = False
except Exception as e:
return False
return bValid
def is_call_valid(path, move_to_path):
# 遍历图像夹下所有图像 root:根目录 dirs:根目录下所有目录(文件夹):files: 包含所有图像的一个list
for 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')
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)