如何在 django 模型中使用 PIL

发布于 2024-10-25 04:20:59 字数 119 浏览 8 评论 0原文

我喜欢在最终存储之前调整上传的图像(ImageField)的大小,我听说 python 有一个名为 PIL 的图像库,我想用它来完成该任务,但我不确定如何开始。

关于如何做有什么建议吗?

谢谢

I like to resize an uploaded image(ImageField) before finally storing, I heard that python has an image library called PIL and I would like to use it do that task but I'm not sure on how to start.

Any suggestions on how to do it?

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身泪倾城 2024-11-01 04:20:59

您可以覆盖模型的保存功能,您可以在其中打开文件并调整其大小(不推荐,因为每次保存模型时它都会调整其大小),您可以在文件上传后调整大小(例如在 form.save() 之前/期间),

但恕我直言更好的解决方案是为此使用专用应用程序,我最喜欢的是 sorl-thumbnails

you can override model's save function where you can open file and resize it (not recommended, as it will resize it each time you save a model), you an resize after file upload (e.g. before/during form.save())

but IMHO a far better solution is to use a dedicated app for this, my favourite is sorl-thumbnails

兮子 2024-11-01 04:20:59

我刚刚知道如何做到这一点,但有没有办法简化它?我是 python 和 django 的新手,所以我不确定这是否是正确的方法。

下面是我的代码:

from django.db.models.signals import pre_delete, pre_save

def on_save_image(sender, **kwargs):
    import PIL
    obj = kwargs['instance']
    if obj.file:
        try:
            original = sender.objects.get(pk = obj.pk)
            if original.file:
                #if change image then delete original file
                original.file.delete()
        except ObjectDoesNotExist:
            pass
        finally:
            img = PIL.Image.open(obj.file)
            img.thumbnail((500, 500))
            # reset pointer to start at 0 again
            obj.file.open()
            img.save(obj.file)
pre_save.connect(on_save_image, sender = Image)

# delete file in memory
def on_delete_image(sender, **kwargs):
    obj = kwargs['instance']
    if obj.file:
        obj.file.delete()
pre_delete.connect(on_delete_image, sender = Image)

谢谢

I've just found out on how to do it but is there a way to simplify it? I'm new in python and django so I'm not sure if this is the proper way to do it.

below is my code:

from django.db.models.signals import pre_delete, pre_save

def on_save_image(sender, **kwargs):
    import PIL
    obj = kwargs['instance']
    if obj.file:
        try:
            original = sender.objects.get(pk = obj.pk)
            if original.file:
                #if change image then delete original file
                original.file.delete()
        except ObjectDoesNotExist:
            pass
        finally:
            img = PIL.Image.open(obj.file)
            img.thumbnail((500, 500))
            # reset pointer to start at 0 again
            obj.file.open()
            img.save(obj.file)
pre_save.connect(on_save_image, sender = Image)

# delete file in memory
def on_delete_image(sender, **kwargs):
    obj = kwargs['instance']
    if obj.file:
        obj.file.delete()
pre_delete.connect(on_delete_image, sender = Image)

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文