how to find height and width of image for FileField Django
Asked Answered
R

1

7

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 ?

Reshape answered 10/11, 2016 at 5:54 Comment(2)
You can change it to an ImageField and use width_field and height_field arguments.Rampant
did you mean ImageField(MModel.objects.all()[0]).height ??Reshape
M
23

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)
Material answered 10/11, 2016 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.