Merge pull request #80 from biszkoptwielki/master

Image resize fails when force flag is set. Fix & unittest
This commit is contained in:
Ross Lawley 2012-08-20 09:34:05 -07:00
commit a2f3d70f28
2 changed files with 27 additions and 3 deletions

View File

@ -1108,6 +1108,7 @@ class ImageGridFsProxy(GridFSProxy):
try: try:
img = Image.open(file_obj) img = Image.open(file_obj)
img_format = img.format
except: except:
raise ValidationError('Invalid image') raise ValidationError('Invalid image')
@ -1142,20 +1143,20 @@ class ImageGridFsProxy(GridFSProxy):
if thumbnail: if thumbnail:
thumb_id = self._put_thumbnail(thumbnail, thumb_id = self._put_thumbnail(thumbnail,
img.format) img_format)
else: else:
thumb_id = None thumb_id = None
w, h = img.size w, h = img.size
io = StringIO() io = StringIO()
img.save(io, img.format) img.save(io, img_format)
io.seek(0) io.seek(0)
return super(ImageGridFsProxy, self).put(io, return super(ImageGridFsProxy, self).put(io,
width=w, width=w,
height=h, height=h,
format=img.format, format=img_format,
thumbnail_id=thumb_id, thumbnail_id=thumb_id,
**kwargs) **kwargs)

View File

@ -1851,6 +1851,29 @@ class FieldTest(unittest.TestCase):
t.image.delete() 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): def test_image_field_thumbnail(self):
if PY3: if PY3:
raise SkipTest('PIL does not have Python 3 support') raise SkipTest('PIL does not have Python 3 support')