how to find height and width of image for FileField Django

半世苍凉 提交于 2019-12-25 04:48:07

问题


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 ?


回答1:


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)


来源:https://stackoverflow.com/questions/40520797/how-to-find-height-and-width-of-image-for-filefield-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!