我知道 StackOverflow 上有无数关于这个错误的问题和答案,我已经阅读了所有这些并尝试了他们的许多建议(Pillow 代替 PIL,io.BytesIO 代替 stringIO),但我仍然遇到同样的问题具有以下内容:
我正在编写代码来处理来自 S3 的图像——特别是将它们重新保存为 JPG,如有必要可以旋转它们,并创建各种缩略图。代码的相关部分获取引用 S3 上图像的 URL 列表。我可以确认,是的,图像位于 URL 指定的位置的 S3 上。在每个 URL 上,我调用以下内容:
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)
此代码在本地运行良好。但是当我在 Heroku 的暂存环境中运行它时,我得到“IOError:无法识别图像文件”。它在 JPG、PNG 和 GIF 上失败,这些都在本地工作。
关于我的环境的细节:
Django==1.5.1
枕头==2.1.0
pilkit==1.1.1
Python 2.7.3(两者上)
本地枕头编译:
--- 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
在 Heroku Pillow 上编译:
*** 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