12

I am trying to read a png file into a python-flask application running in docker and am getting an error that says

ValueError: Could not find a format to read the specified file in mode 'i'

i have uploaded a file using an HTML file and now i am trying to read it for further processing. i see that scipy.misc.imread is deprecated and i am trying to replace this with imageio.imread

if request.method=='POST':
    file = request.files['image']
    if not file: 
        return render_template('index.html', label="No file")
    #img = misc.imread(file)
    img = imageio.imread(file)

i get this error :

File "./appimclass.py", line 34, in make_prediction

img = imageio.imread(file)

File "/usr/local/lib/python3.6/site-packages/imageio/core/functions.py", line 221, in imread

reader = read(uri, format, "i", **kwargs)

File "/usr/local/lib/python3.6/site-packages/imageio/core/functions.py", line 139, in get_reader

"Could not find a format to read the specified file " "in mode %r" % mode
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
Calcutta
  • 1,021
  • 3
  • 16
  • 36
  • have also tried >> img = plt.imread(file) << after >> import matplotlib.pyplot as plt << but even this gives an error : ValueError: Only know how to handle extensions: ['png'] ... even though my uploaded file is png – Calcutta Jan 26 '19 at 15:20
  • tried running the program locally, that is without the docker environment. Now the error is OSError: cannot identify image file – Calcutta Jan 27 '19 at 00:02
  • realised that the problem was with the data and not the code. the PNG files were, for some reason, corrupt. could not even open them with GIMP. hence used a fresh set of data files and everything working fine. – Calcutta Jan 27 '19 at 02:04

6 Answers6

9

Different, but in case helpful. I had an identical error in a different library (skimage), and the solution was to add an extra 'plugin' parameter like so -

image = io.imread(filename,plugin='matplotlib')
Arsene Lupin
  • 343
  • 2
  • 11
7

Had the exact same problem recently, and the issue was a single corrupt file. Best is to use something like PIL to check for bad files.

import os
from os import listdir
from PIL import Image

dir_path = "/path/"


for filename in listdir(dir_path):
    if filename.endswith('.jpg'):
        try:
            img = Image.open(dir_path+"\\"+filename) # open the image file
            img.verify() # verify that it is, in fact an image
        except (IOError, SyntaxError) as e:
            print('Bad file:', filename)
            #os.remove(dir_path+"\\"+filename) (Maybe)
Dipan Ghosh
  • 176
  • 2
  • 9
1

I had this problem today, and found that if I closed the file before reading it into imageio the problem went away.

Error was:

File "/home/vinny/pvenvs/chess/lib/python3.6/site-packages/imageio/core/functions.py", line 139, in get_reader                                                  "Could not find a format to read the specified file " "in mode %r" % mode  ValueError: Could not find a format to read the specified file in mode 'i' 

Solution: Put file.close() before images.append(imageio.imread(filename)), not after.

Uncle Vinny
  • 543
  • 1
  • 4
  • 8
1

Add the option "pilmode":

imageio.imread(filename,pilmode="RGB")

It worked for me.

PabloVD
  • 7
  • 2
  • additional point here: if pilmode="RGB" is used then the image shape changes from (28,28) to (28,28,3). The 3 representing the three channels. Here is the link discussing more on it. https://stackoverflow.com/questions/61552402/if-image-has-28-28-3-shape-how-do-i-convert-it-to-28-28-1 – Kasid Khan Mar 15 '21 at 10:51
1

enter image description here

I encountered the same error, and at last, I found it was because the picture was damaged.

zfireear
  • 53
  • 1
  • 2
  • 8
0

I had accidentally saved some images as PDF, so the error occurred. resolved after deleting those incompatible format images.

Khan
  • 1,288
  • 12
  • 11