diff --git a/django/core/files/images.py b/django/core/files/images.py
index 8883ae4..b234084 100644
a
|
b
|
class ImageFile(File):
|
23 | 23 | |
24 | 24 | def _get_image_dimensions(self): |
25 | 25 | if not hasattr(self, '_dimensions_cache'): |
26 | | close = self.closed |
27 | 26 | self.open() |
28 | | self._dimensions_cache = get_image_dimensions(self, close=close) |
| 27 | self._dimensions_cache = get_image_dimensions(self) |
29 | 28 | return self._dimensions_cache |
30 | 29 | |
31 | 30 | |
32 | | def get_image_dimensions(file_or_path, close=False): |
| 31 | def get_image_dimensions(file_or_path): |
33 | 32 | """ |
34 | | Returns the (width, height) of an image, given an open file or a path. Set |
35 | | 'close' to True to close the file at the end if it is initially in an open |
36 | | state. |
| 33 | Returns the (width, height) of an image, given an open file or a path. If a |
| 34 | file is opened it is closed at the end of the function. |
37 | 35 | """ |
38 | 36 | from PIL import ImageFile as PillowImageFile |
39 | 37 | |
40 | 38 | p = PillowImageFile.Parser() |
| 39 | |
41 | 40 | if hasattr(file_or_path, 'read'): |
42 | 41 | file = file_or_path |
43 | 42 | file_pos = file.tell() |
44 | 43 | file.seek(0) |
| 44 | close = False |
45 | 45 | else: |
46 | 46 | file = open(file_or_path, 'rb') |
47 | 47 | close = True |
| 48 | |
48 | 49 | try: |
49 | 50 | # Most of the time Pillow only needs a small chunk to parse the image |
50 | 51 | # and get the dimensions, but with some TIFF files Pillow needs to |
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index 4208454..daf9ea8 100644
a
|
b
|
class AbstractForeignFieldsModel(models.Model):
|
384 | 384 | |
385 | 385 | class Meta: |
386 | 386 | abstract = True |
| 387 | |
| 388 | |
| 389 | class Profile(models.Model): |
| 390 | image = models.ImageField() |
diff --git a/tests/model_fields/test_imagefield.py b/tests/model_fields/test_imagefield.py
index 7389573..fe42e72 100644
a
|
b
|
class ImageFieldTests(ImageFieldTestMixin, TestCase):
|
181 | 181 | loaded_p = pickle.loads(dump) |
182 | 182 | self.assertEqual(p.mugshot, loaded_p.mugshot) |
183 | 183 | |
| 184 | def test_image_field_io_closed_file(self): |
| 185 | """ |
| 186 | Opening 'image' property as Image object of a model's ImageField will fail |
| 187 | with an 'I/O operation on closed file' error. Regression for #13750 |
| 188 | """ |
| 189 | from .models import Profile |
| 190 | profile = Profile() |
| 191 | profile.image = self.file1 |
| 192 | |
| 193 | profile.save() |
| 194 | |
| 195 | self.assertTrue(profile.image.closed) |
| 196 | |
| 197 | w = profile.image.width |
| 198 | |
| 199 | image = Image.open(profile.image) |
| 200 | |
184 | 201 | |
185 | 202 | @skipIf(Image is None, "Pillow is required to test ImageField") |
186 | 203 | class ImageFieldTwoDimensionsTests(ImageFieldTestMixin, TestCase): |