我正在尝试使用 keras.load_img 加载图像数据集,该图像返回 PIL图像实例,但无法这样做由于某种原因。图片是由python的 imagaug 模块生成的,并通过以下代码行创建,该模块返回 numpy数组:>
img = augmenter(image=image)
img = Image.fromarray(img)
img.save(os.path.join(os.path.dirname(__file__), 'Roll 1_Augmented', 'PaperPrintImgAug' + str(i+1) + '_' + str(j) + '.jpg'))
该图像似乎已成功保存,我可以使用任何图像查看器打开它们。因此,我认为他们没有腐败。但是,每当尝试使用load_img打开图像时,都会出现以下错误:
Traceback (most recent call last):
File "ae.py", line 61, in readImages
img = load_img(folder_path[i], target_size=inputShape)
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/PIL/Image.py", line 2896, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f7350df9e08>
我尝试了在线和stackoverflow上提供的各种解决方案,但情况似乎与我的不匹配。下载最新版本的Pillow后,我在回溯中进行了以下更改:
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/home/ies/billa/miniconda3/envs/pfprint/lib/python3.6/site-packages/PIL/Image.py", line 2006, in open
raise IOError("cannot identify image file")
OSError: cannot identify image file
我怀疑图像编码可能有问题。 有人可以帮我吗?
通过将图像另存为png
文件解决了该问题。
对于JPEG文件问题,以下是我使用的代码:
from tensorflow.python.keras.preprocessing.image import load_img
def readImages(folder_path):
images = []
for i in range(len(folder_path)):
img = load_img(folder_path[i], target_size=inputShape)
img = np.array(img)
images.append(img)
images = np.array(images)
return images
augdata = glob('/home/ies/billa/Roll 1_Augmented/*')
augmentedImages = readImages(augdata)