8
8
================================================================================
9
9
10
10
Save a displayio.Bitmap (and associated displayio.Palette) in a BMP file.
11
- Make a screenshot (the contents of a displayio.Display ) and save in a BMP file.
11
+ Make a screenshot (the contents of a busdisplay.BusDisplay ) and save in a BMP file.
12
12
13
13
14
14
* Author(s): Dave Astels, Matt Land
31
31
import gc
32
32
import struct
33
33
import board
34
- from displayio import Bitmap , Palette , Display , ColorConverter
34
+ from displayio import Bitmap , Palette , ColorConverter
35
+
35
36
36
37
try :
37
38
from typing import Tuple , Optional , Union
38
39
from io import BufferedWriter
40
+ from busdisplay import BusDisplay
41
+ from framebufferio import FramebufferDisplay
39
42
except ImportError :
40
43
pass
41
44
@@ -67,9 +70,11 @@ def _bytes_per_row(source_width: int) -> int:
67
70
return pixel_bytes + padding_bytes
68
71
69
72
70
- def _rotated_height_and_width (pixel_source : Union [Bitmap , Display ]) -> Tuple [int , int ]:
73
+ def _rotated_height_and_width (
74
+ pixel_source : Union [Bitmap , BusDisplay , FramebufferDisplay ]
75
+ ) -> Tuple [int , int ]:
71
76
# flip axis if the display is rotated
72
- if isinstance (pixel_source , Display ) and (pixel_source .rotation % 180 != 0 ):
77
+ if hasattr (pixel_source , "rotation" ) and (pixel_source .rotation % 180 != 0 ):
73
78
return pixel_source .height , pixel_source .width
74
79
return pixel_source .width , pixel_source .height
75
80
@@ -111,7 +116,7 @@ def rgb565_to_rgb888(rgb565):
111
116
# pylint:disable=too-many-locals
112
117
def _write_pixels (
113
118
output_file : BufferedWriter ,
114
- pixel_source : Union [Bitmap , Display ],
119
+ pixel_source : Union [Bitmap , BusDisplay , FramebufferDisplay ],
115
120
palette : Optional [Union [Palette , ColorConverter ]],
116
121
) -> None :
117
122
saving_bitmap = isinstance (pixel_source , Bitmap )
@@ -136,7 +141,7 @@ def _write_pixels(
136
141
color >>= 8
137
142
buffer_index += 1
138
143
else :
139
- # pixel_source: Display
144
+ # pixel_source: display
140
145
result_buffer = bytearray (2048 )
141
146
data = pixel_source .fill_row (y - 1 , result_buffer )
142
147
for i in range (width ):
@@ -156,15 +161,17 @@ def _write_pixels(
156
161
157
162
def save_pixels (
158
163
file_or_filename : Union [str , BufferedWriter ],
159
- pixel_source : Union [Display , Bitmap ] = None ,
164
+ pixel_source : Union [BusDisplay , FramebufferDisplay , Bitmap ] = None ,
160
165
palette : Optional [Union [Palette , ColorConverter ]] = None ,
161
166
) -> None :
162
167
"""Save pixels to a 24 bit per pixel BMP file.
163
168
If pixel_source if a displayio.Bitmap, save it's pixels through palette.
164
- If it's a displayio.Display, a palette isn't required.
169
+ If it's a displayio display, a palette isn't required. To be supported,
170
+ a display must implement `busdisplay.BusDisplay.fill_row`. Known supported
171
+ display types are `busdisplay.BusDisplay` and `framebufferio.FramebufferDisplay`.
165
172
166
173
:param file_or_filename: either the file to save to, or it's absolute name
167
- :param pixel_source: the Bitmap or Display to save
174
+ :param pixel_source: the Bitmap or display to save
168
175
:param palette: the Palette to use for looking up colors in the bitmap
169
176
"""
170
177
if not pixel_source :
@@ -177,8 +184,8 @@ def save_pixels(
177
184
raise ValueError (
178
185
"Third argument must be a Palette or ColorConverter for a Bitmap save"
179
186
)
180
- elif not isinstance (pixel_source , Display ):
181
- raise ValueError ("Second argument must be a Bitmap or Display " )
187
+ elif not hasattr (pixel_source , "fill_row" ):
188
+ raise ValueError ("Second argument must be a Bitmap or supported display type " )
182
189
try :
183
190
if isinstance (file_or_filename , str ):
184
191
output_file = open ( # pylint: disable=consider-using-with
0 commit comments