From 4cc1549b6ebfe8983ee2fd4b9be973448f2d0e0b Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 25 Oct 2019 14:57:37 +0200 Subject: [PATCH] [2.2.x] Fixed #13750 -- Clarified need to reopen models.ImageField.image file to access raw image data. Backport of f57e174fa61e4c31213f6d0033fb9d647b463626 from master --- docs/topics/files.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/topics/files.txt b/docs/topics/files.txt index 67ce56adcd..58bf9b7715 100644 --- a/docs/topics/files.txt +++ b/docs/topics/files.txt @@ -73,6 +73,27 @@ location (:setting:`MEDIA_ROOT` if you are using the default >>> car.photo.path == new_path True +.. note:: + + Whilst :class:`~django.db.models.ImageField` non-image data attributes, + such as ``height``, ``width``, and ``size`` are available on the instance, + the underlying image data cannot be used without reopening the image. For + example:: + + >>> from PIL import Image + >>> car = Car.objects.get(name='57 Chevy') + >>> car.photo.width + 191 + >>> car.photo.height + 287 + >>> image = Image.open(car.photo) + # Raises ValueError: seek of closed file. + >>> car.photo.open() + + >>> image = Image.open(car.photo) + >>> image + + The ``File`` object ===================