Added dithering flag

This commit is contained in:
Casey 2024-09-15 05:42:42 +03:00
parent 89bb807cc2
commit 703a1744b5
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 11 additions and 4 deletions

View File

@ -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: