diff --git a/convert.py b/convert.py index 72cdd36..ff68ec3 100644 --- a/convert.py +++ b/convert.py @@ -59,15 +59,18 @@ def main(): screen = screen_py.DHGR140Screen(palette) lookahead = 0 - image = image_py.open(screen.X_RES, screen.Y_RES, args.input) + image = image_py.open(args.input) + # TODO: better performance with malloc'ed array? + resized = np.array( + image_py.resize(image, screen.X_RES, screen.Y_RES)).astype(np.float32) if args.show_input: - Image.fromarray(image.astype(np.uint8)).show() + Image.fromarray(resized.astype(np.uint8)).show() dither = dither_pattern.PATTERNS[args.dither]() start = time.time() output_4bit, output_rgb = dither_pyx.dither_image( - screen, image, dither, lookahead) + screen, resized, dither, lookahead) print(time.time() - start) screen.pack(output_4bit) @@ -76,7 +79,7 @@ def main(): outfile = os.path.join(os.path.splitext(args.output)[0] + ".png") out_image.save(outfile, "PNG") if args.show_output: - out_image.show() + image_py.resize(out_image, 560, 384, srgb_output=True).show() with open(args.output, "wb") as f: f.write(bytes(screen.main)) diff --git a/image.py b/image.py index 3857535..56e3c90 100644 --- a/image.py +++ b/image.py @@ -21,17 +21,22 @@ def linear_to_srgb(im: np.ndarray) -> np.ndarray: return (np.clip(srgb, 0.0, 1.0) * 255).astype(np.float32) -def open(x_res:int, y_res:int, filename: str) -> np.ndarray: +def open(filename: str) -> np.ndarray: im = Image.open(filename) # TODO: convert to sRGB colour profile explicitly, in case it has some other # profile already. if im.mode != "RGB": im = im.convert("RGB") + return im + +def resize(image: Image, x_res, y_res, srgb_output: bool = False) -> Image: # Convert to linear RGB before rescaling so that colour interpolation is # in linear space - linear = srgb_to_linear(np.asarray(im)).astype(np.uint8) - rescaled = Image.fromarray(linear).resize((x_res, y_res), Image.LANCZOS) - # TODO: better performance with malloc'ed array? - return np.array(rescaled).astype(np.float32) - + linear = srgb_to_linear(np.asarray(image)).astype(np.uint8) + res = Image.fromarray(linear).resize((x_res, y_res), Image.LANCZOS) + if srgb_output: + return Image.fromarray( + linear_to_srgb(np.array(res, dtype=np.float32)).astype(np.uint8)) + else: + return res