Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure if you problem is the same as mine, but I found that accessing the width or height property of a normal Django ImageField would read the file from the storage backend, load it into PIL, and return the dimensions from there. This is especially costly with a remote backend like we're using, and we have very media-heavy pages. </p> <p><a href="https://code.djangoproject.com/ticket/8307" rel="nofollow">https://code.djangoproject.com/ticket/8307</a> was opened to address this but the Django devs closed as wontfix because they want the width and height properties to always return the true values. So I just monkeypatch _get_image_dimensions() to use those fields, which does prevent a large number of the boto messages and improves my page-load times.</p> <p>Below is my code modified from the patch attached to that ticket. I stuck this in a place which gets executed early, such as a models.py.</p> <pre><code>from django.core.files.images import ImageFile, get_image_dimensions def _get_image_dimensions(self): from numbers import Number if not hasattr(self, '_dimensions_cache'): close = self.closed if self.field.width_field and self.field.height_field: width = getattr(self.instance, self.field.width_field) height = getattr(self.instance, self.field.height_field) #check if the fields have proper values if isinstance(width, Number) and isinstance(height, Number): self._dimensions_cache = (width, height) else: self.open() self._dimensions_cache = get_image_dimensions(self, close=close) else: self.open() self._dimensions_cache = get_image_dimensions(self, close=close) return self._dimensions_cache ImageFile._get_image_dimensions = _get_image_dimensions </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload