用PIL保存JPG格式

发布于 2025-01-01 17:05:45 字数 1271 浏览 1 评论 0原文

我正在使用 PIL 制作我上传的图像的缩略图,PNG 或 GIF 一切都很好。然而,上传 JPG 却让我头疼。我有一段时间一直收到无效的格式类型,然后我在 PIL 网站上的 JPG 页面底部发现了这个......

注意:要启用 JPEG 支持,您需要构建并安装 IJG 构建 Python 图像库之前的 JPEG 库。请参阅 有关详细信息,请参阅分发自述文件。

无论如何,所以我部署到 Heroku,由于某种原因,它似乎不再给我在本地遇到的无效格式错误......除了即使现在数据库中有一个照片对象,我可以似乎无法访问它们。我将它们的位置放入图像标签中,但我不断收到损坏的图像链接符号。

这是我的覆盖保存在模型中的样子:

def save(self, force_update=False, force_insert=False, thumb_size=(90,150)):
    image = Image.open(self.image)

    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    # save the original size
    self.image_width, self.image_height = image.size

    image.thumbnail(thumb_size, Image.ANTIALIAS)

    # save the thumbnail to memory
    temp_handle = StringIO()

    image.save(temp_handle, format='JPEG')

    temp_handle.seek(0) # rewind the file
    # save to the thumbnail field
    suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
                            temp_handle.read(),
                            content_type='image/jpg')
    self.thumbnail.save(suf.name, suf, save=False)
    self.thumbnail_width, self.thumbnail_height = image.size


    #save the image object
    super(Photo, self).save(force_update, force_insert)

I'm using PIL to make thumbnails of images I upload and everything is fine with PNGs or GIFs. However, uploading JPGs is giving me a headache. I kept getting a invalid format type for a while, and then I found this at the bottom of the JPG page on the PIL website...

Note: To enable JPEG support, you need to build and install the IJG
JPEG library before building the Python Imaging Library. See the
distribution README for details.

Anyway, so I deployed to Heroku and for some reason it seems to be no longer giving me the invalid format error that I had been getting on my local... except even though there is now a photo object living in the db, I can't seem to access them. I drop their location into into an image tag but I keep getting a broken image link symbol.

Here is what my override save looks like in models:

def save(self, force_update=False, force_insert=False, thumb_size=(90,150)):
    image = Image.open(self.image)

    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    # save the original size
    self.image_width, self.image_height = image.size

    image.thumbnail(thumb_size, Image.ANTIALIAS)

    # save the thumbnail to memory
    temp_handle = StringIO()

    image.save(temp_handle, format='JPEG')

    temp_handle.seek(0) # rewind the file
    # save to the thumbnail field
    suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
                            temp_handle.read(),
                            content_type='image/jpg')
    self.thumbnail.save(suf.name, suf, save=False)
    self.thumbnail_width, self.thumbnail_height = image.size


    #save the image object
    super(Photo, self).save(force_update, force_insert)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文