[Solved]-Django gives "I/O operation on closed file" error when reading from a saved ImageField

21👍

There is a bug in the ImageField which I’ve tracked down and submitted to the django project.

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)

Leave a comment