简体   繁体   中英

Image resize with PIL Error

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM