From 7c8ab9616865797b3bdf68da70dd1a95e01f6157 Mon Sep 17 00:00:00 2001 From: Antoine Cellerier Date: Fri, 18 Oct 2024 20:47:16 +0200 Subject: [PATCH] Fix color for 7in3e 6 color panel Now matches C implementation and documentation --- .../python/lib/waveshare_epd/epd7in3e.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd7in3e.py b/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd7in3e.py index e2014a0b..38178243 100644 --- a/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd7in3e.py +++ b/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd7in3e.py @@ -178,10 +178,17 @@ def init(self): return 0 def getbuffer(self, image): - # Create a pallette with the 7 colors supported by the panel + # Black 0x0 000 + # White 0x1 001 + # Yellow 0x2 010 + # Red 0x3 011 + # Blue 0x5 101 + # Green 0x6 110 + color_map = [0, 1, 2, 3, 5, 6] + + # Create a palette with the 6 colors supported by the panel pal_image = Image.new("P", (1,1)) - pal_image.putpalette( (0,0,0, 255,255,255, 0,255,0, 0,0,255, 255,0,0, 255,255,0) + (0,0,0)*250) - # pal_image.putpalette( (0,0,0, 255,255,255, 0,255,0, 0,0,255, 255,0,0, 255,255,0, 255,128,0) + (0,0,0)*249) + pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0, 0,0,255, 0,255,0) + (0,0,0)*250) # Check if we need to rotate the image imwidth, imheight = image.size @@ -192,16 +199,16 @@ def getbuffer(self, image): else: logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height)) - # Convert the soruce image to the 7 colors, dithering if needed - image_7color = image_temp.convert("RGB").quantize(palette=pal_image) - buf_7color = bytearray(image_7color.tobytes('raw')) + # Convert the source image to the 6 colors, dithering if needed + image_6color = image_temp.convert("RGB").quantize(palette=pal_image) + buf_6color = bytearray(image_6color.tobytes('raw')) # PIL does not support 4 bit color, so pack the 4 bits of color # into a single byte to transfer to the panel buf = [0x00] * int(self.width * self.height / 2) idx = 0 - for i in range(0, len(buf_7color), 2): - buf[idx] = (buf_7color[i] << 4) + buf_7color[i+1] + for i in range(0, len(buf_6color), 2): + buf[idx] = (color_map[buf_6color[i]] << 4) + color_map[buf_6color[i+1]] idx += 1 return buf