示例#1
0
 def crop(self, image, geometry, options):
     """
     Wrapper for ``_crop``
     """
     crop = options['crop']
     if not crop or crop == 'noop':
         return image
     x_image, y_image = self.get_image_size(image)
     x_offset, y_offset = parse_crop(crop, (x_image, y_image), geometry)
     return self._crop(image, geometry[0], geometry[1], x_offset, y_offset)
示例#2
0
def deserialize_options(option_string):
    """ 
       Split out options from option_string and return them as a dict 
       Raise ThumbnailParseError if any syntax errors are discovered.
    """
    options = {}
    bits = iter(smart_split(option_string))
    for bit in bits:
        m = kw_pat.match(bit)
        if not m:
            raise ThumbnailParseError("Invalid thumbnail option: %s" % bit)
        key = smart_str(m.group("key"))
        value = smart_str(m.group("value")).strip("\"'")

        # if the key is "crop" then validate the crop options - raises ThumbnailParseError is invalid
        if key == "crop":
            parse_crop(value, [0, 0], [0, 0])

        options[key] = value
    return options
示例#3
0
    def crop(self, image, options):
        """
        Wrapper for ``_crop``
        """
        crop = options['crop']
        if not crop or crop == 'noop':
            return image

        x_image, y_image = self.get_image_size(image)
        left, top, width, height = parse_crop(crop, (x_image, y_image))
        if left + width > x_image or top + height > y_image:
            return image
        return self._crop(image, width, height, left, top)
示例#4
0
 def crop(self, image, geometry, options):
     """
     Wrapper for ``_crop``
     """
     crop = options['crop']
     if not crop or crop == 'noop':
         return image
     # Smart cropping is suitably different from regular cropping
     # to warrent it's own function
     image_size = self.get_image_size(image)
     if crop == 'smart':
         import logging
         logging.warn('smart')
         return self._entropy_crop(image, geometry[0], geometry[1], image_size[0], image_size[1])
     x_offset, y_offset = parse_crop(crop, image_size, geometry)
     return self._crop(image, geometry[0], geometry[1], x_offset, y_offset)
示例#5
0
    def crop(self, image, geometry, options):
        """
        Wrapper for ``_crop``
        """
        crop = options['crop']
        upscale = options['upscale']
        x_image, y_image = self.get_image_size(image)
        factor = self._calculate_scaling_factor(x_image, y_image, geometry, options)

        if not crop or crop == 'noop' or (not upscale and factor >= 1):
            return image

        geometry = (min(x_image, geometry[0]), min(y_image, geometry[1]))
        x_offset, y_offset = parse_crop(crop, (x_image, y_image), geometry)

        return self._crop(image, geometry[0], geometry[1], x_offset, y_offset)
示例#6
0
    def crop(self, image, geometry, options):
        """
        Wrapper for ``_crop``
        """
        crop = options['crop']
        upscale = options['upscale']
        x_image, y_image = self.get_image_size(image)
        factor = self._calculate_scaling_factor(x_image, y_image, geometry,
                                                options)

        if not crop or crop == 'noop' or (not upscale and factor >= 1):
            return image

        geometry = (min(x_image, geometry[0]), min(y_image, geometry[1]))
        x_offset, y_offset = parse_crop(crop, (x_image, y_image), geometry)

        return self._crop(image, geometry[0], geometry[1], x_offset, y_offset)
示例#7
0
    def crop(self, image, geometry, options):
        """
        Wrapper for ``_crop``
        """
        crop = options['crop']
        x_image, y_image = self.get_image_size(image)

        if not crop or crop == 'noop':
            return image
        elif crop == 'smart':
            # Smart cropping is suitably different from regular cropping
            # to warrent it's own function
            return self._entropy_crop(image, geometry[0], geometry[1], x_image, y_image)

        # Handle any other crop option with the backend crop function.
        geometry = (min(x_image, geometry[0]), min(y_image, geometry[1]))
        x_offset, y_offset = parse_crop(crop, (x_image, y_image), geometry)
        return self._crop(image, geometry[0], geometry[1], x_offset, y_offset)
示例#8
0
    def crop(self, image, geometry, options):
        """
        Wrapper for ``_crop``
        """
        crop = options['crop']
        x_image, y_image = self.get_image_size(image)

        if not crop or crop == 'noop':
            return image
        elif crop == 'smart':
            # Smart cropping is suitably different from regular cropping
            # to warrent it's own function
            return self._entropy_crop(image, geometry[0], geometry[1], x_image, y_image)

        # Handle any other crop option with the backend crop function.
        geometry = (min(x_image, geometry[0]), min(y_image, geometry[1]))
        x_offset, y_offset = parse_crop(crop, (x_image, y_image), geometry)
        return self._crop(image, geometry[0], geometry[1], x_offset, y_offset)
示例#9
0
 def test_px_crop(self):
     crop = parse_crop('200px 33px', (500, 500), (400, 400))
     self.assertEqual(crop, (100, 33))
示例#10
0
 def test_percent_crop(self):
     crop = parse_crop('50% 0%', (500, 500), (400, 400))
     self.assertEqual(crop, (50, 0))
     crop = parse_crop('10% 80%', (500, 500), (400, 400))
     self.assertEqual(crop, (10, 80))
示例#11
0
 def test_alias_crop(self):
     crop = parse_crop('center', (500, 500), (400, 400))
     self.assertEqual(crop, (50, 50))
     crop = parse_crop('right', (500, 500), (400, 400))
     self.assertEqual(crop, (100, 50))
示例#12
0
 def test_px_crop(self):
     crop = parse_crop('200px 33px', (500, 500), (400, 400))
     self.assertEqual(crop, (100, 33))
示例#13
0
 def test_percent_crop(self):
     crop = parse_crop('50% 0%', (500, 500), (400, 400))
     self.assertEqual(crop, (50, 0))
     crop = parse_crop('10% 80%', (500, 500), (400, 400))
     self.assertEqual(crop, (10, 80))
示例#14
0
 def testPercentCrop(self):
     crop = parse_crop("50% 0%", (500, 500), (400, 400))
     self.assertEqual(crop, (50, 0))
     crop = parse_crop("10% 80%", (500, 500), (400, 400))
     self.assertEqual(crop, (10, 80))
示例#15
0
 def testPxCrop(self):
     crop = parse_crop("200px 33px", (500, 500), (400, 400))
     self.assertEqual(crop, (100, 33))
示例#16
0
 def test_alias_crop(self):
     crop = parse_crop('center', (500, 500), (400, 400))
     self.assertEqual(crop, (50, 50))
     crop = parse_crop('right', (500, 500), (400, 400))
     self.assertEqual(crop, (100, 50))
示例#17
0
 def testAliasCrop(self):
     crop = parse_crop("center", (500, 500), (400, 400))
     self.assertEqual(crop, (50, 50))
     crop = parse_crop("right", (500, 500), (400, 400))
     self.assertEqual(crop, (100, 50))