2929from serial .tools .list_ports import comports
3030
3131from library .lcd .lcd_comm import Orientation , LcdComm
32+ from library .lcd .serialize import image_to_BGRA , image_to_BGR , chunked
3233from library .log import logger
3334
3435
@@ -315,15 +316,9 @@ def _generate_full_image(self, image: Image.Image) -> bytes:
315316 elif self .orientation == Orientation .REVERSE_LANDSCAPE :
316317 image = image .rotate (180 )
317318
318- image_data = image .convert ("RGBA" ).load ()
319- image_ret = ''
320- for y in range (image .height ):
321- for x in range (image .width ):
322- pixel = image_data [x , y ]
323- image_ret += f'{ pixel [2 ]:02x} { pixel [1 ]:02x} { pixel [0 ]:02x} { pixel [3 ]:02x} '
319+ bgra_data = image_to_BGRA (image )
324320
325- hex_data = bytearray .fromhex (image_ret )
326- return b'\x00 ' .join (hex_data [i :i + 249 ] for i in range (0 , len (hex_data ), 249 ))
321+ return b'\x00 ' .join (chunked (bgra_data , 249 ))
327322
328323 def _generate_update_image (
329324 self , image : Image .Image , x : int , y : int , count : int , cmd : Optional [Command ] = None
@@ -343,28 +338,26 @@ def _generate_update_image(
343338 elif self .orientation == Orientation .LANDSCAPE :
344339 x0 , y0 = y , x
345340
346- img_raw_data = []
347- image_data = image .convert ("RGBA" ).load ()
348- for h in range (image .height ):
349- img_raw_data .append (f'{ ((x0 + h ) * self .display_height ) + y0 :06x} { image .width :04x} ' )
350- for w in range (image .width ):
351- current_pixel = image_data [w , h ]
352- img_raw_data .append (f'{ current_pixel [2 ]:02x} { current_pixel [1 ]:02x} { current_pixel [0 ]:02x} ' )
341+ img_raw_data = bytearray ()
342+ bgr_data = image_to_BGR (image )
343+ for h , line in enumerate (chunked (bgr_data , image .width * 3 )):
344+ img_raw_data += int (((x0 + h ) * self .display_height ) + y0 ).to_bytes (3 , "big" )
345+ img_raw_data += int (image .width ).to_bytes (2 , "big" )
346+ img_raw_data += line
353347
354- image_msg = '' .join (img_raw_data )
355- image_size = f'{ int ((len (image_msg ) / 2 ) + 2 ):06x} ' # The +2 is for the "ef69" that will be added later.
348+ image_size = int (len (img_raw_data ) + 2 ).to_bytes (3 , "big" ) # The +2 is for the "ef69" that will be added later.
356349
357350 # logger.debug("Render Count: {}".format(count))
358351 payload = bytearray ()
359352
360353 if cmd :
361354 payload .extend (cmd .value )
362- payload .extend (bytearray . fromhex ( image_size ) )
355+ payload .extend (image_size )
363356 payload .extend (Padding .NULL .value * 3 )
364357 payload .extend (count .to_bytes (4 , 'big' ))
365358
366- if len (image_msg ) > 500 :
367- image_msg = '00' .join (image_msg [ i : i + 498 ] for i in range ( 0 , len ( image_msg ), 498 ))
368- image_msg += 'ef69 '
359+ if len (img_raw_data ) > 250 :
360+ img_raw_data = bytearray ( b' \x00 ' ) .join (chunked ( bytes ( img_raw_data ), 249 ))
361+ img_raw_data += b' \xef \x69 '
369362
370- return bytearray . fromhex ( image_msg ) , payload
363+ return img_raw_data , payload
0 commit comments