【问题标题】:Python, PIL and JPEG on HerokuHeroku 上的 Python、PIL 和 JPEG
【发布时间】:2012-04-18 16:12:03
【问题描述】:

我有一个 Django 网站,托管在 Heroku 上。 其中一个模型有一个图像字段,用于获取上传的图像、调整它们的大小并将它们推送到 Amazon S3,以便它们可以持久存储。

这很好用,使用 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

但是,PIL 似乎适用于 PNG 和 GIF,但不兼容 libjpeg。在本地开发环境或完全受控的 'nix 服务器上,只需安装 jpeg 扩展即可。

但是有谁知道是否可以使用 Cedar Heroku 堆栈进行 Jpeg 操作?是否可以在 requirements.txt 中添加其他内容?

在其他不相关的包中,此 virtualenv 的 requirements.txt 包括:

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

谢谢

【问题讨论】:

    标签: python heroku python-imaging-library virtualenv libjpeg


    【解决方案1】:

    我在 requirements.txt 中使用了这个 PIL 分支:

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

    并且可以毫无问题地使用 JPEG:

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

    【讨论】:

    • 非常感谢!效果很好!我确实在让 Heroku 从 mercurial 中提取依赖项时遇到了一些麻烦,所以我将库复制到 github 并链接到那里。
    • 你在 github 上有那个项目的克隆吗?你能指点我吗?
    • 这很好用。这样的方法应该标准化。
    • @errkk 要从 Mercurial 中提取,请在 hg 路径之前添加 hg 作为依赖项。 Current Recommendation
    • @JonTirsen 或者,您可以将项目存档的 URL 拖放到您的需求文件中,即 bitbucket.org/etienned/pil-2009-raclette/get/55d24e80a982.zip 它很快,但不太适合维护;)
    【解决方案2】:

    另外请考虑使用Pillow,“友好的”PIL 分支,它提供:

    • 安装工具兼容性
    • Python 3 兼容性
    • 频繁发布周期
    • 许多错误修复

    【讨论】:

    • Pillow 的优点:Pillow 可以用 pip:pip install pillow 安装。同样,您可以在requirements.txt 中简单地写pillow
    • 现在是 2017 年。pip install pillow 仍然需要通过其他方式预先安装 libjpeg。见this link
    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 2017-03-12
    • 2013-11-10
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多