Пример #1
0
    def test_invalid_image(self) -> None:
        """
        Test invalid image opening.
        """
        image = pygame_menu.BaseImage(
            pygame_menu.baseimage.IMAGE_EXAMPLE_PYTHON)
        self.assertEqual(image.get_size(), (110, 109))

        image._drawing_position = 'invalid'
        self.assertRaises(ValueError, lambda: image._get_position_delta())

        # Test invalid image
        self.assertRaises(
            Exception, lambda: load_pygame_image_file(
                pygame_menu.baseimage.IMAGE_EXAMPLE_PYTHON, test=True))
Пример #2
0
    def __init__(
            self,
            image_path: Union[str, 'Path', 'BytesIO'],
            drawing_mode: int = IMAGE_MODE_FILL,
            drawing_offset: Vector2NumberType = (0, 0),
            drawing_position: str = POSITION_NORTHWEST,
            load_from_file: bool = True,
            frombase64: bool = False,
            image_id: str = ''
    ) -> None:
        super(BaseImage, self).__init__(object_id=image_id)

        assert isinstance(image_path, (str, Path, BytesIO)), \
            'path must be string, Path, or BytesIO object type'
        assert isinstance(load_from_file, bool)
        assert isinstance(frombase64, bool)

        if isinstance(image_path, (str, Path)):
            image_path = str(image_path)
            if not frombase64:
                _, file_extension = path.splitext(image_path)
                file_extension = file_extension.lower()
                assert path.isfile(image_path), \
                    'file {0} does not exist or could not be found, please ' \
                    'check if the path of the image is valid'.format(image_path)
            else:
                file_extension = 'base64'
        else:
            file_extension = 'BytesIO'

        assert file_extension in _VALID_IMAGE_FORMATS, \
            'file extension {0} not valid, please use: {1}' \
            ''.format(file_extension, ','.join(_VALID_IMAGE_FORMATS))

        self._filepath = image_path
        if isinstance(self._filepath, str) and not frombase64:
            self._filename = path.splitext(path.basename(image_path))[0]
        else:
            self._filename = ''
        self._extension = file_extension
        self._frombase64 = frombase64

        # Drawing mode
        self._drawing_mode = 0
        self._drawing_offset = (0, 0)
        self._drawing_position = ''

        self.set_drawing_mode(drawing_mode)
        self.set_drawing_offset(drawing_offset)
        self.set_drawing_position(drawing_position)

        # Convert from bas64 to bytesio
        if frombase64:
            if 'base64,' in image_path:  # Remove header of file
                for i in range(len(image_path)):
                    if image_path[i] == ',':
                        image_path = image_path[(i + 1):]
                        break
            image_path = BytesIO(base64.b64decode(image_path))

        # Load the image and store as a surface
        if load_from_file:
            self._surface = load_pygame_image_file(image_path)
            self._original_surface = self._surface.copy()

        # Other internals
        self._angle = 0
        self._last_transform = (0, 0, None)  # Improves drawing
        self._rotated = False
        self.smooth_scaling = True  # Uses smooth scaling by default in draw() method