Commits

Joseph Tate  committed 1614cd3

Raise an exception if the crop region is 0 or negative for one or both directions

  • Participants
  • Parent commits c92d687

Comments (0)

Files changed (1)

File tw2/upload/model/image.py

 class FileNotOnDisk(Exception):
     pass
 
+class InvalidCropRegion(Exception):
+    pass
+
 log = logging.getLogger(__name__)
 
 '''
         '''
         if not has_PIL:
             raise NotImplementedError('Cannot call crop without having PIL installed')
-        img = PIL.Image.open(self.fs_path)
-        cropped = img.crop((x1, y1, x2, y2))
+        if x2 > x1 and y2 > y1:
+            img = PIL.Image.open(self.fs_path)
+            cropped = img.crop((x1, y1, x2, y2))
 
-        for extname, extent in self.extents.iteritems():
-            res = cropped.resize(extent, PIL.Image.ANTIALIAS)
-            filen = self._fs_file(self._extent_basename(extname), create=True)
-            res.save(filen)
-            os.chmod(filen, self.mode)
-        return True
+            for extname, extent in self.extents.iteritems():
+                res = cropped.resize(extent, PIL.Image.ANTIALIAS)
+                filen = self._fs_file(self._extent_basename(extname), create=True)
+                res.save(filen)
+                os.chmod(filen, self.mode)
+            return True
+        else:
+            raise InvalidCropRegion('The requested crop region is less than or equal to 0 in one or more dimensions')
 
     def maximum_crop_area(self, w, h):
         base_w, base_h = self.extents.values()[-1]