在保存到python Django模型中之前如何调整ImageField图像的大小 [英] How to resize an ImageField image before saving it in python Django model

查看:51
本文介绍了在保存到python Django模型中之前如何调整ImageField图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Django ImageField类函数来调整图像大小,但是我不确定该函数在哪里接受我的新图像尺寸

I am trying to implement a Django ImageField class function for resizing images however I am not certain where this function accepts my new image dimensions

在Linux和Python 3.7上运行

Running on Linux and Python 3.7

我看过这份文档,但是不太明白: https://docs.djangoproject.com/zh/1.11/_modules/django/db/models/fields/files/#ImageField

I've had a look at this documentation, but can't quite make sense of it: https://docs.djangoproject.com/en/1.11/_modules/django/db/models/fields/files/#ImageField

如果有人可以向我展示如何使用此功能的示例,我将不胜感激.

I'd really appreciate it if someone can show me an example of how to use this function.

编辑

我还没有成功调整图像尺寸的大小,这就是我想要达到的目的.鉴于图像是由 ImageField 提取的,在保存该图像之前,我该如何调整其大小(我发现 ImageField update_dimensions_fields 类函数,但是我不能弄清楚如何使用它)

I haven't successfully managed to resize my image dimensions yet, which is what i am trying to achieve. How may I resize this image before I save it given it is being fetched by ImageField (I found the update_dimensions_fields class function for ImageField however i cant figure out how to use it)

class Posts(models.Model):
    title = models.CharField(max_length=200, blank=True)
    body = models.TextField(blank=True)
    created_at = models.DateTimeField(default=datetime.datetime.now)
    post_image = models.ImageField(upload_to=get_image_path, blank=True, null=True)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        # I would like to use the function beneath to resize my images before I save them to my database
        self.post_image.update_dimension_fields(self, instance, force=False, *args, **kwargs)

        super().save(*args, **kwargs) # Call the "real" save() method.

    class Meta:
        verbose_name_plural = "Posts"

推荐答案

您可以使用 django-resized 库.上传时会调整图像大小并为您存储.

You could use the django-resized library. It resizes images when uploaded and stores them for you.

from django_resized import ResizedImageField

class Posts(models.Model):
    title = models.CharField(max_length=200, blank=True)
    body = models.TextField(blank=True)
    created_at = models.DateTimeField(default=datetime.datetime.now)
    post_image = ResizedImageField(size=[500, 300], upload_to=get_image_path, blank=True, null=True)

    def __str__(self):
        return self.title

选项

  • 大小-最大宽度和高度,例如[640,480]
  • 作物-调整大小和裁剪.['top','left']-左上角,['middle',-'center']是中心裁剪,['bottom','right']-裁剪右下角.
  • 质量-调整大小后的图像1..100的质量
  • keep_meta -保留EXIF和其他元数据,默认为True
  • force_format -强制调整大小的图像的格式,可用的格式是枕头支持的格式,默认为无
  • Options

    • size - max width and height, for example [640, 480]
    • crop - resize and crop. ['top', 'left'] - top left corner, ['middle', - 'center'] is center cropping, ['bottom', 'right'] - crop right bottom corner.
    • quality - quality of resized image 1..100
    • keep_meta - keep EXIF and other meta data, default True
    • force_format - force the format of the resized image, available formats are the one supported by pillow, default to None
    • 这篇关于在保存到python Django模型中之前如何调整ImageField图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆