Beispiel #1
0
    def process_image(self, input_file, output_file=None, focal_point=None, backend_name='default'):
        """
        Run this filter on the given image file then write the result into output_file and return it
        If output_file is not given, a new BytesIO will be used instead
        """
        # Get backend
        backend = get_image_backend(backend_name)

        # Parse spec string
        method_name, method_arg = self._method

        # Open image
        input_file.open('rb')
        image = backend.open_image(input_file)
        file_format = image.format

        # Process image
        method = getattr(backend, method_name)
        image = method(image, method_arg, focal_point=focal_point)

        # Make sure we have an output file
        if output_file is None:
            output_file = BytesIO()

        # Write output
        backend.save_image(image, output_file, file_format)

        # Close the input file
        input_file.close()

        return output_file
Beispiel #2
0
    def process_image(self, input_file, output_file=None, focal_point=None, backend_name='default'):
        """
        Run this filter on the given image file then write the result into output_file and return it
        If output_file is not given, a new BytesIO will be used instead
        """
        # Get backend
        backend = get_image_backend(backend_name)

        # Parse spec string
        method_name, method_arg = self._method

        # Open image
        input_file.open('rb')
        image = backend.open_image(input_file)
        file_format = image.format

        # Process image
        method = getattr(backend, method_name)
        image = method(image, method_arg, focal_point=focal_point)

        # Make sure we have an output file
        if output_file is None:
            output_file = BytesIO()

        # Write output
        backend.save_image(image, output_file, file_format)

        # Close the input file
        input_file.close()

        return output_file
Beispiel #3
0
    def get_suggested_focal_point(self, backend_name='default'):
        backend = get_image_backend(backend_name)
        image_file = self.file.file

        # Make sure image is open and seeked to the beginning
        image_file.open('rb')
        image_file.seek(0)

        # Load the image
        image = backend.open_image(self.file.file)
        image_data = backend.image_data_as_rgb(image)

        # Make sure we have image data
        # If the image is animated, image_data_as_rgb will return None
        if image_data is None:
            return

        # Use feature detection to find a focal point
        feature_detector = FeatureDetector(image.size, image_data[0], image_data[1])
        focal_point = feature_detector.get_focal_point()

        # Add 20% extra room around the edge of the focal point
        if focal_point:
            focal_point.width *= 1.20
            focal_point.height *= 1.20

        return focal_point
Beispiel #4
0
    def get_suggested_focal_point(self, backend_name='default'):
        backend = get_image_backend(backend_name)
        image_file = self.file.file

        # Make sure image is open and seeked to the beginning
        image_file.open('rb')
        image_file.seek(0)

        # Load the image
        image = backend.open_image(self.file.file)
        image_data = backend.image_data_as_rgb(image)

        # Make sure we have image data
        # If the image is animated, image_data_as_rgb will return None
        if image_data is None:
            return

        # Use feature detection to find a focal point
        feature_detector = FeatureDetector(image.size, image_data[0],
                                           image_data[1])
        focal_point = feature_detector.get_focal_point()

        # Add 20% extra room around the edge of the focal point
        if focal_point:
            focal_point.width *= 1.20
            focal_point.height *= 1.20

        return focal_point
Beispiel #5
0
    def get_suggested_focal_point(self, backend_name='default'):
        backend = get_image_backend(backend_name)
        image_file = self.file.file

        # Make sure image is open and seeked to the beginning
        image_file.open('rb')
        image_file.seek(0)

        # Load the image
        image = backend.open_image(self.file.file)
        image_data = backend.image_data_as_rgb(image)

        # Make sure we have image data
        # If the image is animated, image_data_as_rgb will return None
        if image_data is None:
            return

        # Use feature detection to find a focal point
        feature_detector = FeatureDetector(image.size, image_data[0],
                                           image_data[1])

        faces = feature_detector.detect_faces()
        if faces:
            # Create a bounding box around all faces
            left = min(face.left for face in faces)
            top = min(face.top for face in faces)
            right = max(face.right for face in faces)
            bottom = max(face.bottom for face in faces)
            focal_point = Rect(left, top, right, bottom)
        else:
            features = feature_detector.detect_features()
            if features:
                # Create a bounding box around all features
                left = min(feature[0] for feature in features)
                top = min(feature[1] for feature in features)
                right = max(feature[0] for feature in features)
                bottom = max(feature[1] for feature in features)
                focal_point = Rect(left, top, right, bottom)
            else:
                return None

        # Add 20% to width and height and give it a minimum size
        x, y = focal_point.centroid
        width, height = focal_point.size

        width *= 1.20
        height *= 1.20

        width = max(width, 100)
        height = max(height, 100)

        return Rect.from_point(x, y, width, height)
Beispiel #6
0
    def get_suggested_focal_point(self, backend_name='default'):
        backend = get_image_backend(backend_name)
        image_file = self.file.file

        # Make sure image is open and seeked to the beginning
        image_file.open('rb')
        image_file.seek(0)

        # Load the image
        image = backend.open_image(self.file.file)
        image_data = backend.image_data_as_rgb(image)

        # Make sure we have image data
        # If the image is animated, image_data_as_rgb will return None
        if image_data is None:
            return

        # Use feature detection to find a focal point
        feature_detector = FeatureDetector(image.size, image_data[0], image_data[1])

        faces = feature_detector.detect_faces()
        if faces:
            # Create a bounding box around all faces
            left = min(face.left for face in faces)
            top = min(face.top for face in faces)
            right = max(face.right for face in faces)
            bottom = max(face.bottom for face in faces)
            focal_point = Rect(left, top, right, bottom)
        else:
            features = feature_detector.detect_features()
            if features:
                # Create a bounding box around all features
                left = min(feature[0] for feature in features)
                top = min(feature[1] for feature in features)
                right = max(feature[0] for feature in features)
                bottom = max(feature[1] for feature in features)
                focal_point = Rect(left, top, right, bottom)
            else:
                return None

        # Add 20% to width and height and give it a minimum size
        x, y = focal_point.centroid
        width, height = focal_point.size

        width *= 1.20
        height *= 1.20

        width = max(width, 100)
        height = max(height, 100)

        return Rect.from_point(x, y, width, height)
Beispiel #7
0
    def process_image(self, input_file, backend_name='default'):
        """
        Given an input image file as a django.core.files.File object,
        generate an output image with this filter applied, returning it
        as another django.core.files.File object
        """
        
        backend = get_image_backend(backend_name)
        
        if not self.method:
            self._parse_spec_string()
        
        # If file is closed, open it
        input_file.open('rb')
        image = backend.open_image(input_file)
        file_format = image.format
        
        method = getattr(backend, self.method_name)

        image = method(image, self.method_arg)

        output = StringIO.StringIO()
        backend.save_image(image, output, file_format)
        
        # and then close the input file
        input_file.close()
        

        # generate new filename derived from old one, inserting the filter spec string before the extension
        input_filename_parts = os.path.basename(input_file.name).split('.')
        filename_without_extension = '.'.join(input_filename_parts[:-1])
        filename_without_extension = filename_without_extension[:60]  # trim filename base so that we're well under 100 chars
        output_filename_parts = [filename_without_extension, self.spec] + input_filename_parts[-1:]
        output_filename = '.'.join(output_filename_parts)

        output_file = File(output, name=output_filename)
        

        return output_file
Beispiel #8
0
    def process_image(self, input_file, backend_name='default'):
        """
        Given an input image file as a django.core.files.File object,
        generate an output image with this filter applied, returning it
        as another django.core.files.File object
        """

        backend = get_image_backend(backend_name)

        if not self.method:
            self._parse_spec_string()

        # If file is closed, open it
        input_file.open('rb')
        image = backend.open_image(input_file)
        file_format = image.format

        method = getattr(backend, self.method_name)

        image = method(image, self.method_arg)

        output = StringIO.StringIO()
        backend.save_image(image, output, file_format)

        # and then close the input file
        input_file.close()

        # generate new filename derived from old one, inserting the filter spec string before the extension
        input_filename_parts = os.path.basename(input_file.name).split('.')
        filename_without_extension = '.'.join(input_filename_parts[:-1])
        filename_without_extension = filename_without_extension[:
                                                                60]  # trim filename base so that we're well under 100 chars
        output_filename_parts = [filename_without_extension, self.spec
                                 ] + input_filename_parts[-1:]
        output_filename = '.'.join(output_filename_parts)

        output_file = File(output, name=output_filename)

        return output_file
Beispiel #9
0
 def test_default_backend(self):
     # default backend should be pillow
     backend = get_image_backend()
     self.assertTrue(isinstance(backend, PillowBackend))
Beispiel #10
0
 def test_default_backend(self):
     # default backend should be pillow
     backend = get_image_backend()
     self.assertTrue(isinstance(backend, PillowBackend))