Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find height and width of image for 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 ?

like image 723
Paras Meena Avatar asked Nov 10 '16 05:11

Paras Meena


People also ask

Which property is used to display the size of an image in Python?

open() is used to open the image and then . width and . height property of Image are used to get the height and width of the image.

What is image field in Django?

ImageField in Django Forms is a input field for upload of image files. The default widget for this input is ClearableFileInput. It normalizes to: An UploadedFile object that wraps the file content and file name into a single object.


1 Answers

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)
like image 93
Burhan Khalid Avatar answered Oct 18 '22 18:10

Burhan Khalid