Example #1
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)
        new_width, new_height = self.calculate((image_width, image_height))

        if (new_width, new_height) == (image_width, image_height):
            return image.copy()

        if not areas:
            x1 = int((image_width - new_width) / 2)
            y1 = int((image_height - new_height) / 2)
        else:
            min_penalty = None
            optimal_coords = None

            for x in xrange(image_width - new_width + 1):
                for y in xrange(image_height - new_height + 1):
                    penalty = 0
                    for area in areas:
                        penalty += self._get_penalty(area, x, y,
                                                     new_width, new_height)
                        if min_penalty is not None and penalty > min_penalty:
                            break

                    if min_penalty is None or penalty < min_penalty:
                        min_penalty = penalty
                        optimal_coords = [(x, y)]
                    elif penalty == min_penalty:
                        optimal_coords.append((x, y))
            x1, y1 = optimal_coords[0]

        x2 = x1 + new_width
        y2 = y1 + new_height

        return image.crop((x1, y1, x2, y2))
Example #2
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)
        new_width, new_height = self.calculate((image_width, image_height))

        if (new_width, new_height) == (image_width, image_height):
            return image.copy()

        if not areas:
            x1 = int((image_width - new_width) / 2)
            y1 = int((image_height - new_height) / 2)
        else:
            min_penalty = None
            optimal_coords = None

            for x in xrange(image_width - new_width + 1):
                for y in xrange(image_height - new_height + 1):
                    penalty = 0
                    for area in areas:
                        penalty += self._get_penalty(area, x, y, new_width,
                                                     new_height)
                        if min_penalty is not None and penalty > min_penalty:
                            break

                    if min_penalty is None or penalty < min_penalty:
                        min_penalty = penalty
                        optimal_coords = [(x, y)]
                    elif penalty == min_penalty:
                        optimal_coords.append((x, y))
            x1, y1 = optimal_coords[0]

        x2 = x1 + new_width
        y2 = y1 + new_height

        return image.crop((x1, y1, x2, y2))
Example #3
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)
        new_width, new_height = self.calculate((image_width, image_height))

        if (new_width, new_height) == (image_width, image_height):
            return image.copy()

        ratiocrop = RatioCrop(ratio="{0}:{1}".format(new_width, new_height))
        new_image = ratiocrop.adjust(image, areas=areas)

        fit = Fit(width=new_width, height=new_height)
        return fit.adjust(new_image)
Example #4
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)
        new_width, new_height = self.calculate((image_width, image_height))

        if (new_width, new_height) == (image_width, image_height):
            return image.copy()

        ratiocrop = RatioCrop(ratio="{0}:{1}".format(new_width, new_height))
        new_image = ratiocrop.adjust(image, areas=areas)

        fit = Fit(width=new_width, height=new_height)
        return fit.adjust(new_image)
Example #5
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)

        if not areas:
            return image.copy()

        for area in areas:
            if area.name == self.kwargs['name']:
                break
        else:
            return image.copy()

        return image.crop((area.x1, area.y1, area.x2, area.y2))
Example #6
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)

        if not areas:
            return image.copy()

        for area in areas:
            if area.name == self.kwargs['name']:
                break
        else:
            return image.copy()

        return image.crop((area.x1, area.y1,
                           area.x2, area.y2))
Example #7
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)
        new_width, new_height = self.calculate((image_width, image_height))

        if (new_width, new_height) == (image_width, image_height):
            return image.copy()

        # Choose a resize filter based on whether
        # we're upscaling or downscaling.
        if new_width < image_width:
            f = Image.ANTIALIAS
        else:
            f = Image.BICUBIC

        return exif_aware_resize(image, (new_width, new_height), f)
Example #8
0
    def adjust(self, image, areas=None):
        image_width, image_height = exif_aware_size(image)
        new_width, new_height = self.calculate((image_width, image_height))

        if (new_width, new_height) == (image_width, image_height):
            return image.copy()

        # Choose a resize filter based on whether
        # we're upscaling or downscaling.
        if new_width < image_width:
            f = Image.ANTIALIAS
        else:
            f = Image.BICUBIC

        return exif_aware_resize(image, (new_width, new_height), f)
 def test_exif_not_rotated(self):
     image = Image.open(self._data_path('20x7_exif_not_rotated.jpg'))
     self.assertEqual(exif_aware_size(image), self.ORIGINAL_ORIENTATION)
 def test_non_exif(self):
     image = Image.open(self._data_path('20x7_no_exif.png'))
     self.assertEqual(exif_aware_size(image), self.ORIGINAL_ORIENTATION)
Example #11
0
 def test_exif_rotated(self):
     image = Image.open(self._data_path('20x7_exif_rotated.jpg'))
     self.assertEqual(exif_aware_size(image), self.ROTATED_ORIENTATION)
Example #12
0
 def test_non_exif(self):
     image = Image.open(self._data_path('20x7_no_exif.png'))
     self.assertEqual(exif_aware_size(image), self.ORIGINAL_ORIENTATION)