How to use the pibooth.pictures.sizing.new_size_keep_aspect_ratio function in pibooth

To help you get started, we’ve selected a few pibooth examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github werdeil / pibooth / pibooth / camera / base.py View on Github external
def get_rect(self):
        """Return a Rect object (as defined in pygame) for resizing preview and images
        in order to fit to the defined window.
        """
        rect = self._window.get_rect()
        res = sizing.new_size_keep_aspect_ratio(self.resolution,
                                                (rect.width - 2 * self._border, rect.height - 2 * self._border))
        return pygame.Rect(rect.centerx - res[0] // 2, rect.centery - res[1] // 2, res[0], res[1])
github werdeil / pibooth / pibooth / camera / gphoto.py View on Github external
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()))
            # 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_hflip:
                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
github werdeil / pibooth / pibooth / pictures / maker.py View on Github external
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]