Skip to content
Permalink
Browse files

allow for only height or width for cropping

  • Loading branch information...
jinpark committed Aug 20, 2015
1 parent d483b62 commit 156ff97a7b233b3d950f651ed32c85596fb69167
Showing with 20 additions and 9 deletions.
  1. +11 −8 app.py
  2. +9 −1 tests/test_app.py
19 app.py
@@ -110,14 +110,17 @@ def resize(img, width=None, height=None):
def crop(img, width=None, height=None, gravity='north_west'):
if not width and not height:
return img
if width and height:
try:
img.crop(width=int(width), height=int(height), gravity=gravity)
except ValueError:
app.logger.exception("cheight: {0} or cwidth: {1} is invalid.".format(height, width))
abort(400, "cheight: {0} or cwidth: {1} is invalid.".format(height, width))
if (width and not height) or (not width and height): # xor!
abort(400, "cheight or cwidth is missing. Both are required for cropping.")
elif width and not height:
height = img.height
elif not width and height:
width = img.width

try:
img.crop(width=int(width), height=int(height), gravity=gravity)
except ValueError:
app.logger.exception("cheight: {0} or cwidth: {1} is invalid.".format(height, width))
abort(400, "cheight: {0} or cwidth: {1} is invalid.".format(height, width))

return img


@@ -89,7 +89,15 @@ def test_crop_bad_width_or_height(self):

def test_crop_only_height(self):
rv = self.app.get('/http://placekitten.com/g/50/200/?cheight=100')
self.assertEqual(rv.status, "400 BAD REQUEST")
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 50)
self.assertEqual(img.height, 100)

def test_crop_only_width(self):
rv = self.app.get('/http://placekitten.com/g/50/200/?cwidth=25')
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 25)
self.assertEqual(img.height, 200)

class TestImageResizerExtras(unittest.TestCase):
def setUp(self):

0 comments on commit 156ff97

Please sign in to comment.
You can’t perform that action at this time.