Convert a PIL image (any mode) to a packed RGB565 byte string. Ragnar renders 1-bit ('1') PIL images internally. This converts any PIL mode to 16-bit RGB565 for the TFT.
(self, image)
| 83 | logger.info("Whisplay cleared") |
| 84 | |
| 85 | def getbuffer(self, image): |
| 86 | """Convert a PIL image (any mode) to a packed RGB565 byte string. |
| 87 | |
| 88 | Ragnar renders 1-bit ('1') PIL images internally. This converts |
| 89 | any PIL mode to 16-bit RGB565 for the TFT. |
| 90 | """ |
| 91 | img = image.convert("RGB") |
| 92 | if img.width != self.width or img.height != self.height: |
| 93 | logger.warning( |
| 94 | "Image size %dx%d → resizing to %dx%d", |
| 95 | img.width, img.height, self.width, self.height, |
| 96 | ) |
| 97 | img = img.resize((self.width, self.height)) |
| 98 | |
| 99 | pixels = img.getdata() |
| 100 | buf = bytearray(self.width * self.height * 2) |
| 101 | idx = 0 |
| 102 | for r, g, b in pixels: |
| 103 | rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3) |
| 104 | buf[idx] = (rgb565 >> 8) & 0xFF |
| 105 | buf[idx + 1] = rgb565 & 0xFF |
| 106 | idx += 2 |
| 107 | return bytes(buf) |
| 108 | |
| 109 | def display(self, buf): |
| 110 | """Write a full-screen RGB565 buffer to the display.""" |