From 703a1744b5253659be258d5edb6802422add4d03 Mon Sep 17 00:00:00 2001 From: hkc Date: Sun, 15 Sep 2024 05:42:42 +0300 Subject: [PATCH] Added dithering flag --- cc-pic.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cc-pic.py b/cc-pic.py index 3b7f840..f605430 100755 --- a/cc-pic.py +++ b/cc-pic.py @@ -74,13 +74,14 @@ class Converter: MAX_DIFF = 3 * 255 - def __init__(self, image: Image.Image, palette: list[int] | int = PALETTE_ADAPTIVE): + def __init__(self, image: Image.Image, palette: list[int] | int = PALETTE_ADAPTIVE, dither: bool = True): + dither_mode = Image.Dither.FLOYDSTEINBERG if dither else Image.Dither.NONE if isinstance(palette, list): img_pal = Image.new("P", (1, 1)) img_pal.putpalette(palette) - self._img = image.quantize(len(palette) // 3, palette=img_pal) + self._img = image.quantize(len(palette) // 3, palette=img_pal, dither=dither_mode) else: - self._img = image.convert("P", palette=palette, colors=16) + self._img = image.convert("P", palette=palette, colors=16, dither=dither_mode) self._imgdata = self._img.load() self._palette: list[int] = self._img.getpalette() or [] @@ -221,6 +222,12 @@ def main(): action="store_true", help="Output a Lua script instead of binary image", ) + parser.add_argument( + "-D", + dest="nodither", + action="store_true", + help="Disable dithering" + ) parser.add_argument( "-W", dest="width", @@ -391,7 +398,7 @@ def main(): else: raise ValueError(f"invalid palette identifier: {args.palette!r}") - converter = Converter(canv, palette) + converter = Converter(canv, palette, dither=not args.nodither) converter._img.save("/tmp/_ccpictmp.png") if args.textmode: with open(args.output_path, "w") as fp: