【问题标题】:how to find height and width of image for FileField Django如何为 FileField Django 查找图像的高度和宽度
【发布时间】:2017-03-24 01:54:33
【问题描述】:

如果我们的模型定义如下,如何找到图像的高度和宽度

class MModel:
 document = FileField()
 format_type = CharField()

并且图像保存在文档中,那么如果它是图像,我们如何找到文档的高度和宽度?

【问题讨论】:

  • 您可以将其更改为ImageField 并使用width_fieldheight_field 参数。
  • 你是说 ImageField(MModel.objects.all()[0]).height ??

标签: python django image models filefield


【解决方案1】:

如果文件始终是图像,请将FileField 更改为ImageField,如下所示:

def MyModel(models.Model):
  height = models.IntegerField()
  width = models.IntegerField()
  document = models.ImageField(height_field='height', width_field='width')

否则,您将不得不手动计算图像的宽度和高度:

from django.core.files.images import get_image_dimensions

obj = MModel.objects.get(pk=1)
width, height = get_image_dimensions(obj.document.file)

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2012-12-25
    • 2011-08-28
    • 2011-06-12
    • 2019-08-21
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多