Added dithering flag
This commit is contained in:
parent
89bb807cc2
commit
703a1744b5
15
cc-pic.py
15
cc-pic.py
|
@ -74,13 +74,14 @@ class Converter:
|
||||||
|
|
||||||
MAX_DIFF = 3 * 255
|
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):
|
if isinstance(palette, list):
|
||||||
img_pal = Image.new("P", (1, 1))
|
img_pal = Image.new("P", (1, 1))
|
||||||
img_pal.putpalette(palette)
|
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:
|
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._imgdata = self._img.load()
|
||||||
self._palette: list[int] = self._img.getpalette() or []
|
self._palette: list[int] = self._img.getpalette() or []
|
||||||
|
@ -221,6 +222,12 @@ def main():
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Output a Lua script instead of binary image",
|
help="Output a Lua script instead of binary image",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-D",
|
||||||
|
dest="nodither",
|
||||||
|
action="store_true",
|
||||||
|
help="Disable dithering"
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-W",
|
"-W",
|
||||||
dest="width",
|
dest="width",
|
||||||
|
@ -391,7 +398,7 @@ def main():
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"invalid palette identifier: {args.palette!r}")
|
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")
|
converter._img.save("/tmp/_ccpictmp.png")
|
||||||
if args.textmode:
|
if args.textmode:
|
||||||
with open(args.output_path, "w") as fp:
|
with open(args.output_path, "w") as fp:
|
||||||
|
|
Loading…
Reference in New Issue