I know there are a zillion questions and answers about this error on StackOverflow, and I have read them all and tried many of their suggestions (Pillow instead of PIL, io.BytesIO instead of stringIO), but I'm still getting the same issue with the following:
I'm working on code to process images from S3 -- specifically to resave them as JPGs, rotate them if necessary, and create various thumbnails. The relevant part of the code gets a list of URLs referring to images on S3. I can confirm that, yes, the images are on S3 in the location specified by the URL. On each URL I call the following:
def process_new_image(file_url):
# Load the image and make it a PIL object
try:
fd = urllib.urlopen(file_url)
image_file = io.BytesIO(fd.read())
image = Image.open(image_file)
except Exception, e:
# ===> This is where the issue happens. <====
raise
# Read the exif data and potentially rotate the image
transpose = Transpose()
transposed_image = transpose.process(image)
# Resave the image as a jpeg
new_image_data = io.BytesIO()
transposed_image.save(new_image_data, 'jpeg', quality=100)
new_image_data.seek(0)
try:
self.image.save('image', ContentFile(new_image_data.read()), save=True)
except Exception, e:
raise
new_image_data.seek(0)
self.process_thumbnails(new_image_data)
This code works fine locally. But when I run it on my staging environment in Heroku, I get "IOError: cannot identify image file." It fails on JPGs, PNGs, and GIFs, which all work locally.
Specifics about my environment:
Django==1.5.1
Pillow==2.1.0
pilkit==1.1.1
Python 2.7.3 (on both)
Locally Pillow compiled with:
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** TIFF G3/G4 (experimental) support not available
--- FREETYPE2 support available
*** LITTLECMS support not available
*** WEBP support not available
On Heroku Pillow compiled with:
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- TIFF G3/G4 (experimental) support available
--- FREETYPE2 support available
--- LITTLECMS support available
*** WEBP support not available