ImageField accessing height or width and then data results in "I/O operation on closed file"
|Reported by:||ROsborne||Owned by:||aethemba|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
Description (last modified by Alex)
If you have a simple model with an ImageField, the following code will fail with a "I/O operation on closed file":
instance = MyClass.objects.get(...) w = instance.image.width h = instance.image.height original = Image.open(instance.image)
The work around is to reopen the file:
instance = MyClass.objects.get(...) w = instance.image.width h = instance.image.height instance.image.open() original = Image.open(instance.image)
Note this is different than the issue of the 2nd read() returning empty strings for two reasons:
- You can not seek(0), the file is closed.
- Needing to reopen in this case is unexpected.
Change History (26)
comment:1 Changed 5 years ago by Alex
- Description modified (diff)
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:7 Changed 6 months ago by aethemba
- Owner changed from nobody to aethemba
- Status changed from new to assigned