예제 #1
0
    def _get_preview_image(self):
        """Capture a new preview image.
        """
        rect = self.get_rect()

        ret, image = self._cam.read()
        if not ret:
            raise IOError("Can not get camera preview image")

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        # Crop to keep aspect ratio of the resolution
        height, width = image.shape[:2]
        cropped = sizing.new_size_by_croping_ratio((width, height), self.resolution)
        image = image[cropped[1]:cropped[3], cropped[0]:cropped[2]]
        # Resize to fit the available space in the window
        height, width = image.shape[:2]
        size = sizing.new_size_keep_aspect_ratio((width, height), (rect.width, rect.height), 'outer')
        image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)

        if self._preview_hflip:
            image = cv2.flip(image, 1)

        if self._overlay is not None:
            image = cv2.addWeighted(image, 1, self._overlay, self._overlay_alpha / 255., 0)

        return Image.fromarray(image)
예제 #2
0
파일: gphoto.py 프로젝트: jo-ei/pibooth
    def _post_process_capture(self, capture_data):
        """Rework capture data.

        :param capture_data: couple (GPhotoPath, effect)
        :type capture_data: tuple
        """
        gp_path, effect = capture_data
        camera_file = self._cam.file_get(gp_path.folder, gp_path.name,
                                         gp.GP_FILE_TYPE_NORMAL)
        if self.delete_internal_memory:
            LOGGER.debug("Delete capture '%s' from internal memory",
                         gp_path.name)
            self._cam.file_delete(gp_path.folder, gp_path.name)
        image = Image.open(io.BytesIO(camera_file.get_data_and_size()))
        image = self._rotate_image(image, self.capture_rotation)

        # Crop to keep aspect ratio of the resolution
        image = image.crop(
            sizing.new_size_by_croping_ratio(image.size, self.resolution))
        # Resize to fit the resolution
        image = image.resize(
            sizing.new_size_keep_aspect_ratio(image.size, self.resolution,
                                              'outer'))

        if self.capture_flip:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)

        if effect != 'none':
            image = image.filter(getattr(ImageFilter, effect.upper()))

        return image
예제 #3
0
def get_pygame_image(name,
                     size=None,
                     antialiasing=True,
                     hflip=False,
                     vflip=False,
                     crop=False,
                     angle=0,
                     invert=False):
    """Return a Pygame image. If a size is given, the image is
    resized keeping the original image's aspect ratio.

    :param name: name of an image located in language folders
    :type name: str
    :param size: resize image to this size
    :type size: tuple
    :param antialiasing: use antialiasing algorithm when resize
    :type antialiasing: bool
    :param hflip: apply an horizontal flip
    :type hflip: bool
    :param vflip: apply a vertical flip
    :type vflip: bool
    :param crop: crop image to fit aspect ration of the size
    :type crop: bool
    :param angle: angle of rotation of the image
    :type angle: int

    :return: pygame.Surface with image
    :rtype: object
    """
    path = get_filename(name)
    if not size:
        image = pygame.image.load(path).convert()
    else:
        if osp.isfile(path):
            pil_image = Image.open(path)
        else:
            pil_image = Image.new('RGBA', size, (0, 0, 0, 0))
        if invert:
            # Generating a RGB image as a RGBA cannot be not inverted
            r, g, b, a = pil_image.split()
            rgb_image = Image.merge('RGB', (r, g, b))
            inverted_image = ImageOps.invert(rgb_image)
            r2, g2, b2 = inverted_image.split()
            pil_image = Image.merge('RGBA', (r2, g2, b2, a))

        if crop:
            pil_image = pil_image.crop(
                sizing.new_size_by_croping_ratio(pil_image.size, size))
        pil_image = pil_image.resize(
            sizing.new_size_keep_aspect_ratio(pil_image.size, size),
            Image.ANTIALIAS if antialiasing else Image.NEAREST)

        image = pygame.image.fromstring(pil_image.tobytes(), pil_image.size,
                                        pil_image.mode)

    if hflip or vflip:
        image = pygame.transform.flip(image, hflip, vflip)
    if angle != 0:
        image = pygame.transform.rotate(image, angle)
    return image
예제 #4
0
파일: opencv.py 프로젝트: chfsx/pibooth
    def _post_process_capture(self, capture_path):
        """Rework and return a Image object from file.
        """
        image, effect = self._captures[capture_path]

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        # Crop to keep aspect ratio of the resolution
        height, width = image.shape[:2]
        cropped = sizing.new_size_by_croping_ratio((width, height),
                                                   self.resolution)
        image = image[cropped[1]:cropped[3], cropped[0]:cropped[2]]
        # Resize to fit the resolution
        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)

        if self._capture_hflip:
            image = cv2.flip(image, 1)

        if effect != 'none':
            pass  # To be implemented

        cv2.imwrite(capture_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
        return Image.fromarray(image)
예제 #5
0
파일: gphoto.py 프로젝트: jo-ei/pibooth
    def _get_preview_image(self):
        """Capture a new preview image.
        """
        rect = self.get_rect()
        if self._preview_compatible:
            cam_file = self._cam.capture_preview()
            image = Image.open(io.BytesIO(cam_file.get_data_and_size()))
            image = self._rotate_image(image, self.preview_rotation)
            # Crop to keep aspect ratio of the resolution
            image = image.crop(
                sizing.new_size_by_croping_ratio(image.size, self.resolution))
            # Resize to fit the available space in the window
            image = image.resize(
                sizing.new_size_keep_aspect_ratio(image.size,
                                                  (rect.width, rect.height),
                                                  'outer'))

            if self.preview_flip:
                image = image.transpose(Image.FLIP_LEFT_RIGHT)
        else:
            image = Image.new('RGB', (rect.width, rect.height),
                              color=(0, 0, 0))

        if self._overlay:
            image.paste(self._overlay, (0, 0), self._overlay)
        return image
예제 #6
0
    def _post_process_capture(self, capture_data):
        """Rework capture data.

        :param capture_data: couple (frame, effect)
        :type capture_data: tuple
        """
        frame, effect = capture_data

        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # Crop to keep aspect ratio of the resolution
        height, width = image.shape[:2]
        cropped = sizing.new_size_by_croping_ratio((width, height),
                                                   self.resolution)
        image = image[cropped[1]:cropped[3], cropped[0]:cropped[2]]
        # Resize to fit the resolution
        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)

        if self._capture_hflip:
            image = cv2.flip(image, 1)

        if effect != 'none':
            LOGGER.warning("Effect with OpenCV camera is not implemented")

        return Image.fromarray(image)
예제 #7
0
파일: __init__.py 프로젝트: sravel/pibooth
def get_pygame_image(name, size=None, antialiasing=True, hflip=False, vflip=False,
                     crop=False, angle=0, color=(255, 255, 255), bg_color=None):
    """Return a Pygame image. If a size is given, the image is
    resized keeping the original image's aspect ratio.

    :param name: name of an image located in language folders
    :type name: str
    :param size: resize image to this size
    :type size: tuple
    :param antialiasing: use antialiasing algorithm when resize
    :type antialiasing: bool
    :param hflip: apply an horizontal flip
    :type hflip: bool
    :param vflip: apply a vertical flip
    :type vflip: bool
    :param crop: crop image to fit aspect ration of the size
    :type crop: bool
    :param angle: angle of rotation of the image
    :type angle: int
    :param color: recolorize the image with this RGB color
    :type color: tuple
    :param bg_color: recolorize the image background with this RGB color
    :type bg_color: tuple

    :return: pygame.Surface with image
    :rtype: object
    """
    path = get_filename(name)
    if not size and not color:
        image = pygame.image.load(path).convert()
    else:
        if osp.isfile(path):
            pil_image = Image.open(path)
        else:
            pil_image = Image.new('RGBA', size, (0, 0, 0, 0))

        if color:
            pil_image = colorize_pil_image(pil_image, color, bg_color)

        if crop:
            pil_image = pil_image.crop(sizing.new_size_by_croping_ratio(pil_image.size, size))
        pil_image = pil_image.resize(sizing.new_size_keep_aspect_ratio(pil_image.size, size),
                                     Image.ANTIALIAS if antialiasing else Image.NEAREST)

        image = pygame.image.frombuffer(pil_image.tobytes(), pil_image.size, pil_image.mode)

    if hflip or vflip:
        image = pygame.transform.flip(image, hflip, vflip)
    if angle != 0:
        image = pygame.transform.rotate(image, angle)
    return image
예제 #8
0
def get_pygame_image(name,
                     size=None,
                     antialiasing=True,
                     hflip=False,
                     vflip=False,
                     crop=False):
    """Return a Pygame image. If a size is given, the image is
    resized keeping the original image's aspect ratio.

    :param name: name of an image located in language folders
    :type name: str
    :param size: resize image to this size
    :type size: tuple
    :param antialiasing: use antialiasing algorithm when resize
    :type antialiasing: bool
    :param hflip: apply an horizontal flip
    :type hflip: bool
    :param vflip: apply a vertical flip
    :type vflip: bool
    :param crop: crop image to fit aspect ration of the size
    :type crop: bool

    :return: pygame.Surface with image
    :rtype: object
    """
    if not size:
        image = pygame.image.load(get_filename(name)).convert()
    else:
        image = Image.open(get_filename(name))
        if crop:
            image = image.crop(
                sizing.new_size_by_croping_ratio(image.size, size))
        image = image.resize(
            sizing.new_size_keep_aspect_ratio(image.size, size),
            Image.ANTIALIAS if antialiasing else Image.NEAREST)
        image = pygame.image.fromstring(image.tobytes(), image.size,
                                        image.mode)

    if hflip or vflip:
        image = pygame.transform.flip(image, hflip, vflip)
    return image
예제 #9
0
파일: gphoto.py 프로젝트: alsobrsp/pibooth
    def _post_process_capture(self, capture_path):
        """Rework and return a Image object from file.
        """
        gp_path, effect = self._captures[capture_path]
        camera_file = self._cam.file_get(gp_path.folder, gp_path.name,
                                         gp.GP_FILE_TYPE_NORMAL)
        image = Image.open(io.BytesIO(camera_file.get_data_and_size()))

        # Crop to keep aspect ratio of the resolution
        image = image.crop(
            sizing.new_size_by_croping_ratio(image.size, self.resolution))
        # Resize to fit the resolution
        image = image.resize(
            sizing.new_size_keep_aspect_ratio(image.size, self.resolution,
                                              'outer'))

        if self._capture_hflip:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)

        if effect != 'none':
            image = image.filter(getattr(ImageFilter, effect.upper()))

        image.save(capture_path)
        return image
예제 #10
0
def test_crop_ratio_original_big_middle():
    coordinates = sizing.new_size_by_croping_ratio((1000, 1200), (800, 600))
    assert coordinates == (0, 225, 1000, 975)
예제 #11
0
def test_crop_ratio_original_big_topleft():
    coordinates = sizing.new_size_by_croping_ratio((1000, 1200), (800, 600),
                                                   'top-left')
    assert coordinates == (0, 0, 1000, 750)
예제 #12
0
def test_crop_ratio_original_small_middle():
    coordinates = sizing.new_size_by_croping_ratio((600, 200), (900, 600))
    assert coordinates == (150, 0, 450, 200)
예제 #13
0
def test_crop_ratio_original_small_left():
    coordinates = sizing.new_size_by_croping_ratio((600, 200), (900, 600),
                                                   'top-left')
    assert coordinates == (0, 0, 300, 200)