]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/ondiskbitmap.py
Add pre-commit support.
[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 PIL import Image
21
22 __version__ = "0.0.0-auto.0"
23 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
24
25
26 class OnDiskBitmap:
27     """
28     Loads values straight from disk. This minimizes memory use but can lead to much slower
29     pixel load times. These load times may result in frame tearing where only part of the
30     image is visible."""
31
32     def __init__(self, file):
33         self._image = Image.open(file).convert("RGBA")
34
35     @property
36     def width(self):
37         """Width of the bitmap. (read only)"""
38         return self._image.width
39
40     @property
41     def height(self):
42         """Height of the bitmap. (read only)"""
43         return self._image.height
44
45     def __getitem__(self, index):
46         """
47         Returns the value at the given index. The index can either be
48         an x,y tuple or an int equal to `y * width + x`.
49         """
50         if isinstance(index, (tuple, list)):
51             x = index[0]
52             y = index[1]
53         elif isinstance(index, int):
54             x = index % self._image._width
55             y = index // self._image._width
56         if not 0 <= x < self._image.width or not 0 <= y < self._image.height:
57             return 0
58
59         return self._image.getpixel((x, y))