From 18349a053f917de4557a6be5953f6ec7151d749d Mon Sep 17 00:00:00 2001 From: ptone Date: Fri, 4 Nov 2011 00:11:22 -0700 Subject: [PATCH] Try to better handle being passed a open file with write mode enabled such as would be the case with tempfile.mkstemp etc --- imagekit/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/imagekit/utils.py b/imagekit/utils.py index 07cd532..f758e88 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -1,3 +1,4 @@ +import os import tempfile import types @@ -31,6 +32,11 @@ def get_spec_files(instance): def open_image(target): + if hasattr(target, 'mode') and 'w' in target.mode and os.path.exists(target.name): + # target is a file like object with write mode enabled + # PIL will zero this file out and then give and IO error + # instead just pass PIL the path + target = target.name img = Image.open(target) img.copy = types.MethodType(_wrap_copy(img.copy), img, img.__class__) return img