如何为FileField Django查找图像的高度和宽度 [英] how to find height and width of image for FileField Django
本文介绍了如何为FileField Django查找图像的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们的模型定义如下,如何查找图像的高度和宽度
How to find height and width of image if our model is defined as follow
class MModel:
document = FileField()
format_type = CharField()
并将图像保存在文档中,那么如果它是图像,我们如何找到文档的高度和宽度呢?
and image is saved in document then how we can find height and width of a document if it is image ?
推荐答案
If the files will always be images, change FileField
to ImageField
, like this:
def MyModel(models.Model):
height = models.IntegerField()
width = models.IntegerField()
document = models.ImageField(height_field='height', width_field='width')
否则,您将必须手动计算图像的宽度和高度:
Otherwise, you'll have to manually calculate the image width and height:
from django.core.files.images import get_image_dimensions
obj = MModel.objects.get(pk=1)
width, height = get_image_dimensions(obj.document.file)
这篇关于如何为FileField Django查找图像的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文