]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/_ondiskbitmap.py
return palette from OnDiskBitamp.pixel_shader
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / _ondiskbitmap.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `displayio.ondiskbitmap`
7 ================================================================================
8
9 displayio for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19
20 from typing import Union, BinaryIO
21 from PIL import Image
22 from ._colorconverter import ColorConverter
23 from ._palette import Palette
24
25 __version__ = "0.0.0-auto.0"
26 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
27
28
29 class OnDiskBitmap:
30     """
31     Loads values straight from disk. This minimizes memory use but can lead to much slower
32     pixel load times. These load times may result in frame tearing where only part of the
33     image is visible."""
34
35     def __init__(self, file: Union[str, BinaryIO]):
36         self._image = Image.open(file).convert("RGBA")
37
38     @property
39     def width(self) -> int:
40         """Width of the bitmap. (read only)"""
41         return self._image.width
42
43     @property
44     def height(self) -> int:
45         """Height of the bitmap. (read only)"""
46         return self._image.height
47
48     @property
49     def pixel_shader(self) -> Union[ColorConverter, Palette]:
50         """Height of the bitmap. (read only)"""
51         return self._image.getpalette()
52
53     def __getitem__(self, index: Union[tuple, list, int]) -> int:
54         """
55         Returns the value at the given index. The index can either be
56         an x,y tuple or an int equal to `y * width + x`.
57         """
58         if isinstance(index, (tuple, list)):
59             x = index[0]
60             y = index[1]
61         elif isinstance(index, int):
62             x = index % self._image._width
63             y = index // self._image._width
64         if not 0 <= x < self._image.width or not 0 <= y < self._image.height:
65             return 0
66
67         return self._image.getpixel((x, y))