0

I'm doing machine learning project and I got some trouble using Python and PIL. I downloaded some images from google and I'm trying to resize them using PIL, but i got an error, which i don't understand and i do not know what to do.

path = '.../Dataset'

for folder_name in breeds:
    for image in os.listdir(path + '/' + folder_name):
        img = Image.open(path + '/' + folder_name + '/' + image)
        new_width  = 200
        new_height = 200
        img = img.resize((new_width, new_height), Image.ANTIALIAS)
        img = img.convert("RGB")
        img.save(path + '/' + folder_name + '/' + image)

I want to load the image and then resize it so all the images I have are fixed sized (200,200). (All the images i downloaded with the crawler are at least 200x200). I was getting some strange error but after googleing it i saw that i have to convert the image to RGB. After that i try to save it. After processing like 30-40k images i got this error :

.../.local/lib/python3.6/site-packages/PIL/Image.py:916: UserWarning: Palette images with Transparency   expressed in bytes should be converted to RGBA images
  'to RGBA images')
.../.local/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:742: UserWarning: Corrupt EXIF data.  Expecting to read 12 bytes but only got 6. 
  warnings.warn(str(msg))
.../.local/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:742: UserWarning: Corrupt EXIF data.  Expecting to read 12 bytes but only got 10. 
  warnings.warn(str(msg))
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-3-6016015ffe6f> in <module>()
      6         new_width  = 200
      7         new_height = 200
----> 8         img = img.resize((new_width, new_height), Image.ANTIALIAS)
      9         img = img.convert("RGB")
     10         img.save(path + '/' + folder_name + '/' + image)

~/.local/lib/python3.6/site-packages/PIL/Image.py in resize(self, size, resample, box)
   1743             return self.convert('RGBa').resize(size, resample, box).convert('RGBA')
   1744 
-> 1745         self.load()
   1746 
   1747         return self._new(self.im.resize(size, resample, box))

~/.local/lib/python3.6/site-packages/PIL/ImageFile.py in load(self)
    231                             else:
    232                                 raise IOError("image file is truncated "
--> 233                                               "(%d bytes not processed)" % len(b))
    234 
    235                         b = b + s

OSError: image file is truncated (9 bytes not processed)

Any ideas how to deal with that? Thanks!

Zarrie
  • 325
  • 2
  • 16
  • Possible duplicate of [Python PIL "IOError: image file truncated" with big images](https://stackoverflow.com/q/24087669/6622817) – Taku Feb 04 '18 at 17:57
  • I read the topic you suggest, but i still do not know if it solves my problem. Can i for example catch the exception i get and remove the image which causes the problem? – Zarrie Feb 04 '18 at 18:17
  • 1
    Can you post the image, or an URL to the image? I'd like to reproduce the problem. – Michiel Overtoom Feb 05 '18 at 15:02

1 Answers1

0

You certainly can catch the OSError that is thrown, like this:

#!/usr/bin/env python3

from PIL import Image
import os

try:
    fn = "earthmap.tiff"
    im = Image.open(fn)
    im = im.resize(200, 200)
    im.save("eartmap-output.tiff")
except OSError:
    print(f"Error processing '{fn}', deleting...")
    os.unlink(fn)
Michiel Overtoom
  • 1,609
  • 13
  • 14