在AWS Elastic Beanstalk上使用Pillow时出现"decoder jpeg not available"错误

8
我是一名有用的助手,可以为您翻译文本。
我在AWS Elastic Beanstalk下使用Python处理JPEG文件遇到了一些问题。
在.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进入我的实例,它就可以正常工作,而且我已经尝试重新构建环境了。
我做错了什么?
更新:
如建议所述,我再次通过pip (/opt/python/run/venv/bin/pip) SSH到实例中,并重新安装了Pillow,在此之前,我确保libjpeg-devel在Pillow之前已经存在于环境中。
我运行了selftest.py并确认我已经支持jpeg。因此,在最后一次尝试中,我转到Elastic Beanstalk界面上的“重新启动应用程序服务器”。 它奏效了。
谢谢大家。

2
你是在安装Pillow之前还是之后安装了libjpeg库? - kylieCatt
1
根据我的经验,在安装Pillow时必须先安装libjpeg。如果你确定已经安装了libjpeg,请尝试卸载Pillow并重新安装。 - kylieCatt
2
在处理 requirements.txt 文件之前,需要先安装软件包。您能否快照记录以查看日志中是否出现任何错误? http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.loggingS3.title.html - Rohit Banga
1
@PedroAlves:请您在下面的答案框中添加您的答案,这样就不会显示为未回答,其他人也可以更快地找到解决方案。 - Hugo
1
@Hugo 当然。已经完成了。 - Pedro Alves
显示剩余2条评论
2个回答

8

根据这里的一般建议,我通过在我的.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"

3

如建议所示,我再次通过SSH进入实例,并通过pip(/opt/python/run/venv/bin/pip)重新安装了Pillow,在此之前,我确认在安装Pillow之前已经确保环境中有libjpeg-devel。

我运行了selftest.py,确认我已经支持jpeg。因此,在最后一次尝试中,我在Elastic Beanstalk界面上点击了“重启应用服务器”。这个方法行得通了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接