#remove bad images
#encoding:utf-8
## Used to find dirty pictures
import os
import re
import sys
from skimage import io,transform
from PIL import Image
# from PIL import ImageFile
# ImageFile.LOAD_TRUNCATED_IMAGES = True
print(1, "Warning:")
print(Image.MAX_IMAGE_PIXELS)
print(2, "No warning:")
Image.MAX_IMAGE_PIXELS = None
print(Image.MAX_IMAGE_PIXELS)
input_path = sys.argv[1]
remove_list = []
two_dimension = []
not_three_dimension = []
truncated_image = []
file_list = os.listdir(input_path)
for index,file_name in enumerate(file_list):
file_path = os.path.join(input_path,file_name)
print("is reading: ",index, file_path)
if re.findall(".jpg",file_path):
try:
img_file2 = io.imread(file_path)
if (len(img_file2.shape) == 2):
print("two dimension", file_name)
print("two dimension", img_file2.shape)
two_dimension.append(file_name)
elif (len(img_file2.shape) != 3):
print("not three dimension", file_name)
print("not three dimension", img_file2.shape)
not_three_dimension.append(file_name)
elif (img_file2.shape[0]*img_file2.shape[1]*img_file2.shape[2] >= 178956970) or (img_file2.shape[2] > 3):
print("images over pixels or not RGB", file_name)
print("images over pixels or not RGB", img_file2.shape)
remove_list.append(file_name)
except Exception as e:
truncated_image.append(file_name)
print("image is truncated", file_name)
print("image is truncated", img_file2.shape)
print("truncated_image", truncated_image)
print("two_dimension", two_dimension)
print("not_three_dimension", not_three_dimension)
print("remove_list", remove_list)
for remove_file_name in two_dimension :
remove_file_path = os.path.join(input_path, remove_file_name)
os.remove(remove_file_path)
print("rm " + remove_file_path)
for remove_file_name in not_three_dimension:
remove_file_path = os.path.join(input_path, remove_file_name)
os.remove(remove_file_path)
print("rm " + remove_file_path)
for remove_file_name in remove_list:
remove_file_path = os.path.join(input_path, remove_file_name)
os.remove(remove_file_path)
print("rm " + remove_file_path)
for remove_file_name in truncated_image:
remove_file_path = os.path.join(input_path, remove_file_name)
os.remove(remove_file_path)
print("rm " + remove_file_path)
remove bad images
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- LeetCode 26. Remove Duplicates from Sorted Array Given a ...
- 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素。css中主要有一下几种选择器: 元素选择器类选择器I...