How to Get the Width and Height of a Photo Uploaded in Django



Python


In this article, we show how to get the width and height attributes of a photo uploaded in Django.

This can be very useful because you may choose to adjust code based on the size attributes of the photo that is uploaded.

Below is the code that allows us to extract the width and height attributes of a photo in Django.



So in order to save the width and height attributes in the django model (if this is what you want to do), you should create width and height columns within the model.

Then you can create within the save() function, the values of the width and height, as shown above.

If your goal is to have the width and height data within the views.py and not necessarily within django model code, then you can get this in a similar way.



So if you are using a form, where the user uploads a photo, within the code for the form, we can extract the width and height attributes using the same get_image_dimensions() function.

If saving to a database, we can do this at the this time as well.

And this is how we can get the width and height attributes of a photo in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...