From 0d1f1bb7431c437274c42684a53b06a20d117fd3 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 21 Nov 2023 13:47:26 +0000 Subject: [PATCH] Add auto dither to what. --- examples/7color/image.py | 28 +++++++++++++++++++--------- inky/inky_ssd1608.py | 18 +++++++++++++++--- inky/inky_ssd1683.py | 14 +++++++++++++- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/examples/7color/image.py b/examples/7color/image.py index 62fcc956..3d2ab1cc 100755 --- a/examples/7color/image.py +++ b/examples/7color/image.py @@ -1,25 +1,35 @@ #!/usr/bin/env python3 +import argparse +import pathlib import sys from PIL import Image from inky.auto import auto +parser = argparse.ArgumentParser() + +parser.add_argument("--saturation", "-s", type=float, default=0.5, help="Colour palette saturation") +parser.add_argument("--file", "-f", type=pathlib.Path, help="Image file") + inky = auto(ask_user=True, verbose=True) -saturation = 0.5 -if len(sys.argv) == 1: - print(""" -Usage: {file} image-file -""".format(file=sys.argv[0])) +args, _ = parser.parse_known_args() + +saturation = args.saturation + +if not args.file: + print(f"""Usage: + {sys.argv[0]} --file image.png (--saturation 0.5)""") sys.exit(1) -image = Image.open(sys.argv[1]) +image = Image.open(args.file) resizedimage = image.resize(inky.resolution) -if len(sys.argv) > 2: - saturation = float(sys.argv[2]) +try: + inky.set_image(resizedimage, saturation=saturation) +except TypeError: + inky.set_image(resizedimage) -inky.set_image(resizedimage, saturation=saturation) inky.show() diff --git a/inky/inky_ssd1608.py b/inky/inky_ssd1608.py index cab464ee..cfca079c 100644 --- a/inky/inky_ssd1608.py +++ b/inky/inky_ssd1608.py @@ -261,9 +261,21 @@ def set_border(self, colour): def set_image(self, image): """Copy an image to the display.""" - canvas = Image.new("P", (self.rows, self.cols)) - canvas.paste(image, (self.offset_x, self.offset_y)) - self.buf = numpy.array(canvas, dtype=numpy.uint8).reshape((self.cols, self.rows)) + if not image.mode == "P": + palette_image = Image.new("P", (1, 1)) + r, g, b = 0, 0, 0 + if self.colour == "red": + r = 255 + if self.colour == "yellow": + g = 255 + palette_image.putpalette([255, 255, 255, 0, 0, 0, r, g, b] + [0, 0, 0] * 252) + image.load() + image = image.im.convert("P", True, palette_image.im) + + canvas = Image.new("P", (self.cols, self.rows)) + width, height = image.size + canvas.paste(image, (self.offset_x, self.offset_y, width, height)) + self.buf = numpy.array(canvas, dtype=numpy.uint8).reshape((self.rows, self.cols)) def _spi_write(self, dc, values): """Write values over SPI. diff --git a/inky/inky_ssd1683.py b/inky/inky_ssd1683.py index 0c263bb0..cf18946e 100644 --- a/inky/inky_ssd1683.py +++ b/inky/inky_ssd1683.py @@ -249,8 +249,20 @@ def set_border(self, colour): def set_image(self, image): """Copy an image to the display.""" + if not image.mode == "P": + palette_image = Image.new("P", (1, 1)) + r, g, b = 0, 0, 0 + if self.colour == "red": + r = 255 + if self.colour == "yellow": + g = 255 + palette_image.putpalette([255, 255, 255, 0, 0, 0, r, g, b] + [0, 0, 0] * 252) + image.load() + image = image.im.convert("P", True, palette_image.im) + canvas = Image.new("P", (self.cols, self.rows)) - canvas.paste(image, (self.offset_x, self.offset_y)) + width, height = image.size + canvas.paste(image, (self.offset_x, self.offset_y, width, height)) self.buf = numpy.array(canvas, dtype=numpy.uint8).reshape((self.rows, self.cols)) def _spi_write(self, dc, values):