#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...