From 9a4ed78ca6f400fbfb05bfce8898bc526df068f3 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 12 Mar 2025 10:55:21 -0500 Subject: [PATCH 1/2] add a way to specify resample method for xdisplay_mirror --- examples/xdisplay_mirror.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/examples/xdisplay_mirror.py b/examples/xdisplay_mirror.py index 45a4f84..8d55668 100644 --- a/examples/xdisplay_mirror.py +++ b/examples/xdisplay_mirror.py @@ -20,20 +20,35 @@ This example command will mirror a 128x128 pixel square from the top left of the import click import numpy as np -from PIL import ImageGrab +from PIL import Image, ImageGrab import adafruit_blinka_raspberry_pi5_piomatter as piomatter import adafruit_blinka_raspberry_pi5_piomatter.click as piomatter_click from adafruit_blinka_raspberry_pi5_piomatter.pixelmappers import simple_multilane_mapper +RESAMPLE_MAP = { + "nearest": Image.NEAREST, + "bilinear": Image.BILINEAR, + "lanczos": Image.LANCZOS, + "bicubic": Image.BICUBIC +} + @click.command @click.option("--mirror-region", help="Region of X display to mirror. Comma seperated x,y,w,h. " "Default will mirror entire display.", default="") @click.option("--x-display", help="The X display to mirror. Default is :0", default=":0") +@click.option("--resample-method", + help="The resample method for PIL to use when resizing the screen image." + "Valid values are: nearest, bilinear, lanczos, and bicubic. Default is nearest", + default="nearest") @piomatter_click.standard_options(n_lanes=2, n_temporal_planes=0) def main(width, height, serpentine, rotation, pinout, n_planes, - n_temporal_planes, n_addr_lines, n_lanes, mirror_region, x_display): + n_temporal_planes, n_addr_lines, n_lanes, mirror_region, x_display, resample_method): + + if resample_method not in RESAMPLE_MAP.keys(): + raise ValueError(f"--resample-method must be one of: {RESAMPLE_MAP.keys()}") + if n_lanes != 2: pixelmap = simple_multilane_mapper(width, height, n_addr_lines, n_lanes) geometry = piomatter.Geometry(width=width, height=height, n_planes=n_planes, n_addr_lines=n_addr_lines, @@ -56,13 +71,14 @@ def main(width, height, serpentine, rotation, pinout, n_planes, while True: img = ImageGrab.grab(xdisplay=x_display) if mirror_region is not None: - img = img.crop((mirror_region[0], mirror_region[1], # left,top - mirror_region[0] + mirror_region[2], # right + img = img.crop((mirror_region[0], mirror_region[1], # left,top + mirror_region[0] + mirror_region[2], # right mirror_region[1] + mirror_region[3])) # bottom - img = img.resize((width, height)) + img = img.resize((width, height), RESAMPLE_MAP[resample_method]) framebuffer[:, :] = np.array(img) matrix.show() + if __name__ == '__main__': main() From 270c8b854cba612f6861fc62266802c4055eb0dd Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 12 Mar 2025 11:47:21 -0500 Subject: [PATCH 2/2] use click.Choice --- examples/xdisplay_mirror.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/xdisplay_mirror.py b/examples/xdisplay_mirror.py index 8d55668..16d35a6 100644 --- a/examples/xdisplay_mirror.py +++ b/examples/xdisplay_mirror.py @@ -38,17 +38,12 @@ RESAMPLE_MAP = { @click.option("--mirror-region", help="Region of X display to mirror. Comma seperated x,y,w,h. " "Default will mirror entire display.", default="") @click.option("--x-display", help="The X display to mirror. Default is :0", default=":0") -@click.option("--resample-method", - help="The resample method for PIL to use when resizing the screen image." - "Valid values are: nearest, bilinear, lanczos, and bicubic. Default is nearest", - default="nearest") +@click.option("--resample-method", type=click.Choice(RESAMPLE_MAP), default="nearest", + help="The resample method for PIL to use when resizing the screen image. Default is nearest") @piomatter_click.standard_options(n_lanes=2, n_temporal_planes=0) def main(width, height, serpentine, rotation, pinout, n_planes, n_temporal_planes, n_addr_lines, n_lanes, mirror_region, x_display, resample_method): - if resample_method not in RESAMPLE_MAP.keys(): - raise ValueError(f"--resample-method must be one of: {RESAMPLE_MAP.keys()}") - if n_lanes != 2: pixelmap = simple_multilane_mapper(width, height, n_addr_lines, n_lanes) geometry = piomatter.Geometry(width=width, height=height, n_planes=n_planes, n_addr_lines=n_addr_lines,