ValueError('Only know how to handle PNG; with Pillow ' 'installed, Matplotlib can handle more images')

Viewed 280

I'm new to Python and I'm trying to load a series of images as an array. All of the images are in the same folder.

Here's the code I'm using.

import os
from os import listdir
from matplotlib import image, pyplot

loaded_images = list()
for filename in listdir('UFO-120/train_val/lrd'):
    # load image
    lr_train_data = image.imread(filename, format='JPG')
    # store loaded image
    loaded_images.append(lr_img_data)
    print('> loaded %s %s' % (filename, lr_train_data.shape))

Here's the entire error message.

FileNotFoundError                         Traceback (most recent call last)

<ipython-input-4-81fe8e3a7102> in <module>()
6 for filename in listdir('UFO-120/train_val/lrd'):
7         # load image
----> 8         lr_train_data = image.imread(filename, format='JPG')
9         # store loaded image
10         loaded_images.append(lr_img_data)

1 frames

/usr/local/lib/python3.6/dist-packages/matplotlib/image.py in imread(fname, format)
1462             raise ValueError('Only know how to handle PNG; with Pillow '
1463                              'installed, Matplotlib can handle more images')
-> 1464         with Image.open(fname) as image:
1465             return pil_to_array(image)
1466     from matplotlib import _png

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2807 
2808     if filename:
-> 2809         fp = builtins.open(filename, "rb")
2810         exclusive_fp = True
2811 

FileNotFoundError: [Errno 2] No such file or directory: 'set_o406.jpg'

The error seems to be telling me that all of the files need to have the .png file extension, but I thought using the matplotlib image.imread would resolve that. I also added the format='JPG' declaration but that didn't make a difference.

As to the File Not Found Error, 'set_o406.jpg' is a valid file name in the folder.

The folder does contain 1500 images and 'set_o406.jpg' is # 754/1500, is it possible that I'm exceeding the memory that can be loaded into a single array? If so, can you recommend a way to work around that?

I'd appreciate any help you're able to provide. Thank you.

1 Answers

I have run the code and found the issue. So, i made some modifications to your program according to that :-

1st Modification:- Added path of your directory to a variable imgDir.

2nd Modification:- Replaced filename with imgDir+filename. So, your program can find your file. you cannot find the file without the path of its directory.

3rd Modification:- You have made a silly mistake, instead of writing

loaded_images.append(lr_train_data)

you wrote

loaded_images.append(lr_img_data)

So, finally your code looks like this:-

import os
from os import listdir
from matplotlib import image, pyplot

imgDir = 'UFO-120/train_val/lrd/'

loaded_images = list()
for filename in listdir(imgDir):
    # load image
    lr_train_data = image.imread(imgDir+filename, format='JPG')
    # store loaded image
    loaded_images.append(lr_train_data)
    print('> loaded %s %s' % (imgDir+filename, lr_train_data.shape))
Related