From 676a7bf712f014b2b3f96e05862dffc818d3e05c Mon Sep 17 00:00:00 2001 From: mikolaj Date: Sat, 18 Aug 2012 17:27:22 +0100 Subject: [PATCH] Image resize fails when Froce flag is set --- mongoengine/fields.py | 7 ++++--- tests/test_fields.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index e6c65f55..ef20d892 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -1108,6 +1108,7 @@ class ImageGridFsProxy(GridFSProxy): try: img = Image.open(file_obj) + img_format = img.format except: raise ValidationError('Invalid image') @@ -1142,20 +1143,20 @@ class ImageGridFsProxy(GridFSProxy): if thumbnail: thumb_id = self._put_thumbnail(thumbnail, - img.format) + img_format) else: thumb_id = None w, h = img.size io = StringIO() - img.save(io, img.format) + img.save(io, img_format) io.seek(0) return super(ImageGridFsProxy, self).put(io, width=w, height=h, - format=img.format, + format=img_format, thumbnail_id=thumb_id, **kwargs) diff --git a/tests/test_fields.py b/tests/test_fields.py index 82099382..ff9246b8 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1851,6 +1851,29 @@ class FieldTest(unittest.TestCase): t.image.delete() + def test_image_field_resize_force(self): + if PY3: + raise SkipTest('PIL does not have Python 3 support') + + class TestImage(Document): + image = ImageField(size=(185, 37, True)) + + TestImage.drop_collection() + + t = TestImage() + t.image.put(open(TEST_IMAGE_PATH, 'r')) + t.save() + + t = TestImage.objects.first() + + self.assertEquals(t.image.format, 'PNG') + w, h = t.image.size + + self.assertEquals(w, 185) + self.assertEquals(h, 37) + + t.image.delete() + def test_image_field_thumbnail(self): if PY3: raise SkipTest('PIL does not have Python 3 support')