15

I have a Django site, hosted on Heroku. One of the models has an image field, that takes uploaded images, resizes them, and pushes them to Amazon S3 so that they can be stored persistently.

This is working well, using PIL

def save(self, *args, **kwargs):


    # Save this one
    super(Product, self).save(*args,**kwargs)

    # resize on file system
    size = 200, 200
    filename = str(self.thumbnail.path)
    image = Image.open(filename)
    image.thumbnail(size, Image.ANTIALIAS)
    image.save(filename)

    # send to amazon and remove from ephemeral file system
    if put_s3(filename):
        os.remove(filename)
        return True

However, PIL seems to work fine for PNGs and GIFs, but is not compliled with libjpeg. On a local development environment or a fully controlled 'nix server, it is simply a case of installing the jpeg extension.

But does anyone know whether Jpeg manipulation is possible using the Cedar Heroku stack? Is there something else that can be added to requirements.txt?

Among other unrelated packages, the requirements.txt for this virtualenv includes:

Django==1.3.1
PIL==1.1.7
distribute==0.6.24
django-queued-storage==0.5
django-storages==1.1.4
psycopg2==2.4.4
python-dateutil==1.5
wsgiref==0.1.2

Thanks

errkk
  • 305
  • 2
  • 9

2 Answers2

22

I use this PIL fork in requirements.txt:

-e hg+https://bitbucket.org/etienned/pil-2009-raclette/#egg=PIL

and can use JPEG without issues:

       --------------------------------------------------------------------
       PIL 1.2a0 SETUP SUMMARY
       --------------------------------------------------------------------
       version       1.2a0
       platform      Python 2.7.2 (default, Oct 31 2011, 16:22:04)
                     [GCC 4.4.3] on linux2
       --------------------------------------------------------------------
       *** TKINTER support not available
       --- JPEG support available
       *** WEBP support not available
       --- ZLIB (PNG/ZIP) support available
       --- FREETYPE2 support available
       --- LITTLECMS support available
       --------------------------------------------------------------------
Mike
  • 505
  • 5
  • 13
  • 3
    Thank you so much! that worked perfectly! I did have a bit of trouble getting Heroku to pull a dependancy from mercurial, so I copied the library to github and linked to there. – errkk May 12 '12 at 14:23
  • 2
    You have a clone of that project on github? Can you point me to it please? – Jon Tirsen Jun 06 '12 at 16:39
  • This works nicely. An approach like this should be standardized. – Pier1 Sys Jul 03 '12 at 04:04
  • 1
    @errkk To pull from Mercurial add `hg` as a dependency before the hg path. [Current Recommendation](https://github.com/heroku/heroku-buildpack-python/issues/59#issuecomment-9414364) – saul.shanabrook Oct 14 '12 at 16:02
  • @JonTirsen alternatively you can just drop the URL to project archive into your requirements file, i.e. https://bitbucket.org/etienned/pil-2009-raclette/get/55d24e80a982.zip it's quick, but not quite good for maintenance ;) – wik Oct 31 '12 at 20:56
  • @errkk as far I see mercurial must be installed auto magically now: https://github.com/heroku/heroku-buildpack-python/blob/master/bin/compile#L203 – wik Oct 31 '12 at 22:51
  • Wow the Heroku guys really should take a note of this, such a standard thing for Django devs, it should be included on every enviro. – holografix Dec 25 '12 at 23:08
  • @rpod Isn't this the result you're after? "JPEG support available" means your PIL package supports JPEG, which is the goal here. – Mike Oct 19 '13 at 17:30
9

Also please consider using Pillow, the "friendly" PIL fork which offers:

  • Setuptools compatibility
  • Python 3 compatibility
  • Frequent release cycle
  • Many bug fixes
aclark
  • 4,345
  • 1
  • 19
  • 31
  • Advantages of Pillow: Pillow can be installed with pip: `pip install pillow`. Similarly, you can simply write `pillow` in the `requirements.txt`. – Martin Thoma Apr 09 '16 at 08:01
  • 3
    It's now the year 2017. `pip install pillow` still requires the prior installation of `libjpeg` by other means. See [this link](https://pillow.readthedocs.io/en/latest/installation.html#external-libraries) . – Mike O'Connor Feb 15 '17 at 08:11