Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch PIL.UnidentifiedImageError in except

I have a huge Zip file with hundreds of thousands of images. I am trying to read those images in memory and extract the pixel data into an array. The idea is to use the flattened image array in model building. PIL.Image is not able to read a few files and it raises UnidenfitiedImageError in such cases. I want to catch this in try-except and take the path of all the problem images into a separate list. I am new to python & programming. I tried with a try-except clause but it does not work. Please help:

with ZipFile('/XXXXX/YYYYY/ZZZZZ/AI_ML/Project2/words.zip') as myzip:
  contents = myzip.namelist()
  for i in range(0,len(contents)-1):
    text = str(contents[i])
    if '.png' in text:
      if 'MACOSX' not in text:
        file_paths.append(contents[i])
  for path in file_paths:
    img = myzip.read(path)

    try:
      img_data = Image.open(BytesIO(img))
    except UnidentifiedImageError:
      problem_files.append(path)

    img_data = img_data.convert('L')              # 2
    image_as_array = np.array(img_data, np.uint8) # 3
    img_list.append(image_as_array)

This gives an error -

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f63ef5154c0>

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
<ipython-input-8-23281c9e5d49> in <module>()
     16     try:
     17       img_data = Image.open(BytesIO(img))
---> 18     except UnidentifiedImageError:
     19       problem_files.append(path)
     20     img_data = img_data.convert('L')              # 2

NameError: name 'UnidentifiedImageError' is not defined
like image 961
Raja Malyala Avatar asked Oct 21 '25 07:10

Raja Malyala


1 Answers

You're getting a NameError because UnidentifiedImageError isn't define in your namespace. By checking the documenation, you can see that you can access the exception in PIL.UnidentifiedImageError, so you can either insert

from PIL import UnidentifiedImageError

at the start of the code, or alternatively just import PIL, and

try:
    img_data = Image.open(BytesIO(img))
except PIL.UnidentifiedImageError:
    problem_files.append(path)

Finally, because UnidentifiedImageError inherits from OSError, you can also write except OSError:, although it's best to be narrower rather than broader when specifying which exceptions to catch so other issues don't go unnoticed, such as other exceptions raised from opening the file.

like image 83
xavc Avatar answered Oct 23 '25 21:10

xavc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!