如果我们的模型定义如下,如何找到图像的高度和宽度
class MModel:
document = FileField()
format_type = CharField()
并且图像保存在文档中,那么如果它是图像,我们如何找到文档的高度和宽度?
如果文件始终是图像,请更改FileField
to ImageField https://docs.djangoproject.com/en/1.10/ref/models/fields/#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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)