2016-11-10 115 views
0

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

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

和图像保存在文档中,我们又如何能找到的高度和宽度图像的高度和宽度如果它是图像文件的文件?

+1

你可以将其更改为“ImageField”并使用“width_field”和“height_field”参数。 – Selcuk

+0

您的意思是ImageField(MModel.objects.all()[0])。height? –

回答

4

如果这些文件将永远是图像,改变FileFieldImageField,像这样:

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)