在AWS Elastic Beanstalk上使用Pillow时出现“解码JPEG不可用”
我在AWS Elastic Beanstalk上用Python处理jpeg文件时遇到了一些麻烦。
我在.ebextensions/python.config文件里写了这些:
packages:
yum:
libjpeg-turbo-devel: []
libpng-devel: []
freetype-devel: []
...
所以我相信我已经安装并正常使用libjpeg(我试过libjpeg-devel,但yum找不到这个包)。
另外,我在requirements.txt里有这些:
Pillow==2.5.1
...
所以我相信我在我的环境中已经安装并正常使用Pillow。
然后,因为我有Pillow和libjpeg,我试着在一个Python脚本中使用PIL.Image并保存到文件。像这样:
from PIL import Image
def resize_image(image,new_size,crop=False,correctOrientationSize=False):
assert type(new_size) == dict
assert new_size.has_key('width') and new_size.has_key('height')
THUM_SIZE = [new_size['width'],new_size['height']]
file_like = cStringIO.StringIO(base64.decodestring(image))
thumbnail = Image.open(file_like)
(width,height) = thumbnail.size
if correctOrientationSize and height > width:
THUM_SIZE.reverse()
thumbnail.thumbnail(THUM_SIZE)
if crop:
# Recorta imagem
thumbnail = crop_image(thumbnail)
output = cStringIO.StringIO()
thumbnail.save(output,format='jpeg')
return output.getvalue().encode('base64')
但是,当我在Elastic Beanstalk的实例上运行它时,调用.save()方法时出现了“decoder jpeg not available”的异常。
如果我通过SSH进入我的实例,它就能正常工作,我已经尝试重建环境了。
我到底做错了什么?
更新:
按照建议,我再次通过SSH进入实例,并通过pip重新安装了Pillow(/opt/python/run/venv/bin/pip),在此之前我确保libjpeg-devel在Pillow之前就已经在环境中了。
我运行了selftest.py,确认我有jpeg的支持。所以,最后我在Elastic Beanstalk界面上点击了“重启应用服务器”。这次成功了。
谢谢大家。
2 个回答
按照建议,我又通过SSH连接到服务器,然后通过pip重新安装了Pillow(使用的是/opt/python/run/venv/bin/pip),在这之前我确保环境中已经安装了libjpeg-devel。
我运行了selftest.py,结果确认我支持jpeg格式。所以,最后我在Elastic Beanstalk界面上点击了“重启应用服务器”。这次成功了。
根据这里的建议,我通过在我的 .ebextensions 配置中添加以下内容,并重新部署来解决这个问题。
packages:
yum:
libjpeg-turbo-devel: []
libpng-devel: []
freetype-devel: []
container_commands:
...
05_uninstall_pil:
command: "source /opt/python/run/venv/bin/activate && yes | pip uninstall Pillow"
06_reinstall_pil:
command: "source /opt/python/run/venv/bin/activate && yes | pip install Pillow --no-cache-dir"