Example #1
0
    def _get_preview_capture(self):
        """Capture a new preview image.
        """
        with timeit('Capturing new preview image'):
            camera_file = self._cam.capture_preview()
            file_data = gp.check_result(
                gp.gp_file_get_data_and_size(camera_file))
            image = Image.open(io.BytesIO(memoryview(file_data)))
            if self._preview_hflip:
                image = image.transpose(Image.FLIP_LEFT_RIGHT)
            if self._rotation:
                image = image.rotate(self._rotation)

            image = image.resize(
                sizing.new_size_keep_aspect_ratio(image.size, self.resolution,
                                                  'outer'))
            image = image.crop(
                sizing.new_size_by_croping(image.size, self.resolution))

        # Resize to the window rect (outer because rect already resized innner, see 'get_rect')
        rect = self.get_rect()
        return image.resize(
            sizing.new_size_keep_aspect_ratio(image.size,
                                              (rect.width, rect.height),
                                              'outer'))
Example #2
0
 def _fit_to_resolution(self, image):
     """Resize and crop to fit the desired resolution.
     """
     # Same process as RPI camera (almost), this is for keeping
     # final rendering sizing (see pibooth.picutres.maker)
     image = image.resize(
         sizing.new_size_keep_aspect_ratio(image.size, self.resolution,
                                           'outer'))
     return image.crop(
         sizing.new_size_by_croping(image.size, self.resolution))
Example #3
0
 def _image_resize_keep_ratio(self, image, max_w, max_h, crop=False):
     """See upper class description.
     """
     if crop:
         width, height = sizing.new_size_keep_aspect_ratio(image.size, (max_w, max_h), 'outer')
         image = image.resize((width, height), Image.ANTIALIAS)
         image = image.crop(sizing.new_size_by_croping(image.size, (max_w, max_h)))
     else:
         width, height = sizing.new_size_keep_aspect_ratio(image.size, (max_w, max_h), 'inner')
         image = image.resize((width, height), Image.ANTIALIAS)
     return image, image.size[0], image.size[1]
Example #4
0
 def _fit_to_resolution(self, image):
     """Resize and crop to fit the desired resolution.
     """
     # Same process as RPI camera (almost), this is for keeping
     # final rendering sizing (see pibooth.picutres.maker)
     height, width = image.shape[:2]
     size = sizing.new_size_keep_aspect_ratio((width, height), self.resolution, 'outer')
     image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
     height, width = image.shape[:2]
     cropped = sizing.new_size_by_croping((width, height), self.resolution)
     return image[cropped[1]:cropped[3], cropped[0]:cropped[2]]
Example #5
0
    def _post_process_capture(self, capture_path):
        gp_path = self._captures[capture_path]
        camera_file = gp.check_result(gp.gp_camera_file_get(
            self._cam, gp_path.folder, gp_path.name, gp.GP_FILE_TYPE_NORMAL))

        image = Image.open(io.BytesIO(memoryview(camera_file.get_data_and_size())))
        image = image.resize(sizing.new_size_keep_aspect_ratio(image.size, self.resolution, 'outer'), Image.ANTIALIAS)
        image = image.crop(sizing.new_size_by_croping(image.size, self.resolution))

        if self._capture_hflip:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)
        image.save(capture_path)
        return image
Example #6
0
def test_crop_original_big_middle():
    coordinates = sizing.new_size_by_croping((1000, 1200), (800, 600))
    assert coordinates == (100, 300, 900, 900)
Example #7
0
def test_crop_original_big_topleft():
    coordinates = sizing.new_size_by_croping((1000, 1200), (800, 600),
                                             'top-left')
    assert coordinates == (0, 0, 800, 600)
Example #8
0
def test_crop_original_small_middle():
    coordinates = sizing.new_size_by_croping((600, 200), (900, 600))
    assert coordinates == (-150, -200, 750, 400)
Example #9
0
def test_crop_original_small_left():
    coordinates = sizing.new_size_by_croping((600, 200), (900, 600),
                                             'top-left')
    assert coordinates == (0, 0, 900, 600)