I am trying to make a simple function in python that can take in a filepath and an outputfilepath, and then make a 64x64 thumbnail for the image found at filepath and save the thumbnail to outputfilepath. Here is my entire code:
def create_thumbnail2(filepath, outputpath):
if not os.path.exists(filepath):
print "Input file path for create_thumbnail doesn't exist. Returning None"
return None
try:
size = 64, 64 #Will be making a 64x64 thumbnail
im = Image.open(filepath)
print "image successfully opened"
im.thumbnail(size, Image.ANTIALIAS)
print "made thumbnail"
im.save(outputpath, "PNG") #Save image as a PNG
return outputpath
except IOError:
print "I/O error"
return None
print "TEST 1"
filep = "test_images/cat1.jpg"
print create_thumbnail2(filep, "test_images/cat1_thumbnail.png")
print "\nTEST 2"
filep = "test_images/cat2.jpg"
print create_thumbnail2(filep, "test_images/cat2_thumbnail.png")
The problem is that this code will work fine for some images, but will raise an IOError on the line where I call "im.thumbnail(size, Image.ANTIALIAS)". Here is the output of the above program.
TEST 1
image successfully opened
I/O error
None
TEST 2
image successfully opened
made thumbnail
test_images/cat2_thumbnail.png
You'll notice that in the first test, the an I/O Error is thrown after the image is opened but before a thumbnail is created. In the second test no error is thrown, and the thumbnail is actually successfully saved to the outputpath. No matter what order I call the two different tests in, or if I comment one out and run the other one alone, the result is always TEST 1 failing and TEST 2 succeeding. Both cat1.jpg and cat2.jpg seem to be valid JPEG images, I can't really find anything different between them besides the file names and the actual picture content.
In case anyone wants to try it with my images, I downloaded cat1 from here: http://dellone2one.com/wp-content/uploads/2009/11/angry_wet_cat.jpg
and I downloaded cat2 from here: http://cvcl.mit.edu/hybrid/cat2.jpg
EDITED TO ADD THE FULL TRACEBACK WITHOUT THE HANDLING: Here is the full traceback
Traceback (most recent call last):
File "image_utils.py", line 75, in <module>
print create_thumbnail2(filep, "test_images/cat1_thumbnail.png")
File "image_utils.py", line 66, in create_thumbnail2
im.thumbnail(size, Image.ANTIALIAS)
File "/Users/dylan/arcode/python/arcode/PIL/Image.py", line 1559, in thumbnail
self.load()
File "/Users/dylan/arcode/python/arcode/PIL/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/Users/dylan/arcode/python/arcode/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available