Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"decoder jpeg not available" with Pillow on AWS Elastic Beanstalk

i'm having some trouble to handle jpeg files on Python under AWS Elastic Beanstalk.

I have this on .ebextensions/python.config file:

packages:
 yum:
  libjpeg-turbo-devel: []
  libpng-devel: []
  freetype-devel: []
...

So i believe i have libjpeg installed and working (i tried libjpeg-devel, but yum can't find this package).

Also, i have this on my requirements.txt:

Pillow==2.5.1
...

So i believe i have Pillow installed and working on my environment.

Then, since i have Pillow and libjpeg, i'm trying to do some work using PIL.Image in a Python script and save to a file. Like this:

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')

However, when i try to run it on Elastic Beanstalk's instance, the exception "decoder jpeg not available" when it calls .save() method.

If i SSH into my instance, it works just fine and i already tried to rebuild the environment.

What am i doing wrong?

UPDATE:

As suggested, i SSHed again into the instance and reinstalled Pillow through pip (/opt/python/run/venv/bin/pip), not before i has had sure libjpeg-devel was on environment before Pillow.

I ran selftest.py and it confirmed that i had support for jpeg. So, in a last try, i went to "Restart App Server" on Elastic Beanstalk interface. It worked.

Thank you all.

like image 553
Pedro Alves Avatar asked Jul 30 '14 18:07

Pedro Alves


2 Answers

Following the general advice from here, I solved this by adding the following in my .ebextensions configuration and re-deploying.

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"
like image 74
Mav3rick Avatar answered Oct 12 '22 10:10

Mav3rick


As suggested, I SSHed again into the instance and reinstalled Pillow through pip (/opt/python/run/venv/bin/pip), not before I has had sure libjpeg-devel was on environment before Pillow.

I ran selftest.py and it confirmed that I had support for jpeg. So, in a last try, I went to "Restart App Server" on Elastic Beanstalk interface. It worked.

like image 27
Pedro Alves Avatar answered Oct 12 '22 10:10

Pedro Alves