用于解决下载图像读取错误,exif问题,与判断图像重新定义大小是否会出错问题,并找出出错图像。
import piexif
import os
from PIL import Image
# 图像存放绝对地址
original_dataset_dir='/home/lyuncxw/AI/bird/bird1'
# 重定义图像大小,元组
target_size = [300,300]
width_height_tuple = (target_size[1], target_size[0])
# 重采样滤波器参数
'''
fill_mode: One of {"constant", "nearest", "reflect" or "wrap"}.
Default is 'nearest'.
Points outside the boundaries of the input are filled
according to the given mode:
- 'constant': kkkkkkkk|abcd|kkkkkkkk (cval=k)
- 'nearest': aaaaaaaa|abcd|dddddddd
- 'reflect': abcddcba|abcd|dcbaabcd
- 'wrap': abcdabcd|abcd|abcdabcd
'''
# interpolation='nearest' #即 -> pil_image.NEAREST
# resample = _PIL_INTERPOLATION_METHODS[interpolation]
# 利用format()函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字
# 符代替'%'
fnames = ['bird1.{}.jpg'.format(i) for i in range(1000)]
# 循环抛出异常
for fname in fnames:
try:
src = os.path.join(original_dataset_dir, fname)
piexif.remove(src) # 去除图片exif
img = Image.open(src) # 打开图片
img = img.resize(width_height_tuple, Image.NEAREST)
# img = img.resize(width_height_tuple, pil_image.NEAREST)
except IOError:
print(src)