Accessing image dimensions on an ImageField in a Django template?

olegmil picture olegmil · Apr 2, 2011 · Viewed 19.3k times · Source

I have ImageField in my model and I'm able to display the image in a template. However, how do I retrieve the image's height and width?

Answer

bradley.ayers picture bradley.ayers · Apr 2, 2011

See the documentation for ImageField.

In addition to the special attributes that are available for FileField, an ImageField also has height and width attributes.

So just do the following:

<img src="{{ image.url }}" width="{{ image.width }}" height="{{ image.height }}"/>