我在 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() 方法时出现异常“解码器 jpeg 不可用”。
如果我通过 SSH 连接到我的实例,它工作正常并且我已经尝试重建环境。
我做错了什么?
更新:
按照建议,我再次通过 SSH 连接到实例并通过 pip (/opt/python/run/venv/bin/pip) 重新安装了 Pillow,而不是在我确定 libjpeg-devel 在 Pillow 之前的环境中。
我运行了 selftest.py,它确认我支持 jpeg。因此,在最后一次尝试中,我转到了 Elastic Beanstalk 界面上的“重新启动应用程序服务器”。有效。
谢谢大家
最佳答案
遵循 here 的一般建议,我通过在我的 .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"
关于python - "decoder jpeg not available"在 AWS Elastic Beanstalk 上使用 Pillow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043982/