88================================================================================
99
1010Save 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.
1212
1313
1414* Author(s): Dave Astels, Matt Land
3131import gc
3232import struct
3333import board
34- from displayio import Bitmap , Palette , Display , ColorConverter
34+ from displayio import Bitmap , Palette , ColorConverter
35+
3536
3637try :
3738 from typing import Tuple , Optional , Union
3839 from io import BufferedWriter
40+ from busdisplay import BusDisplay
41+ from framebufferio import FramebufferDisplay
3942except ImportError :
4043 pass
4144
@@ -67,9 +70,11 @@ def _bytes_per_row(source_width: int) -> int:
6770 return pixel_bytes + padding_bytes
6871
6972
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 ]:
7176 # 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 ):
7378 return pixel_source .height , pixel_source .width
7479 return pixel_source .width , pixel_source .height
7580
@@ -111,7 +116,7 @@ def rgb565_to_rgb888(rgb565):
111116# pylint:disable=too-many-locals
112117def _write_pixels (
113118 output_file : BufferedWriter ,
114- pixel_source : Union [Bitmap , Display ],
119+ pixel_source : Union [Bitmap , BusDisplay , FramebufferDisplay ],
115120 palette : Optional [Union [Palette , ColorConverter ]],
116121) -> None :
117122 saving_bitmap = isinstance (pixel_source , Bitmap )
@@ -136,7 +141,7 @@ def _write_pixels(
136141 color >>= 8
137142 buffer_index += 1
138143 else :
139- # pixel_source: Display
144+ # pixel_source: display
140145 result_buffer = bytearray (2048 )
141146 data = pixel_source .fill_row (y - 1 , result_buffer )
142147 for i in range (width ):
@@ -156,15 +161,17 @@ def _write_pixels(
156161
157162def save_pixels (
158163 file_or_filename : Union [str , BufferedWriter ],
159- pixel_source : Union [Display , Bitmap ] = None ,
164+ pixel_source : Union [BusDisplay , FramebufferDisplay , Bitmap ] = None ,
160165 palette : Optional [Union [Palette , ColorConverter ]] = None ,
161166) -> None :
162167 """Save pixels to a 24 bit per pixel BMP file.
163168 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`.
165172
166173 :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
168175 :param palette: the Palette to use for looking up colors in the bitmap
169176 """
170177 if not pixel_source :
@@ -177,8 +184,8 @@ def save_pixels(
177184 raise ValueError (
178185 "Third argument must be a Palette or ColorConverter for a Bitmap save"
179186 )
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 " )
182189 try :
183190 if isinstance (file_or_filename , str ):
184191 output_file = open ( # pylint: disable=consider-using-with
0 commit comments