colors: Early graphicsmagick support.

This commit is contained in:
Dylan Araps 2017-12-29 20:41:15 +11:00
parent b37d70bdbf
commit feff9c17ba

View file

@ -19,16 +19,29 @@ def imagemagick(color_count, img):
elif shutil.which("convert"):
magick_command = ["convert"]
elif shutil.which("gm"):
magick_command = ["gm", "convert"]
else:
print("error: imagemagick not found, exiting...\n"
"error: wal requires imagemagick to function.")
sys.exit(1)
colors = subprocess.run([*magick_command, img,
"-resize", "25%", "+dither",
"-colors", str(color_count),
"-unique-colors", "txt:-"],
stdout=subprocess.PIPE)
magick_command = ["gm", "convert"]
def magick(magic="-unique-colors"):
"""Run Imagemagick."""
return subprocess.run([*magick_command, img,
"-resize", "25%", "+dither",
"-colors", str(color_count),
magic, "txt:-"],
stdout=subprocess.PIPE)
if magick_command[0] == "gm":
colors = magick("")
else:
colors = magick()
return colors.stdout.splitlines()
@ -52,6 +65,14 @@ def gen_colors(img, color_count):
"for the image. Exiting...")
sys.exit(1)
if len(raw_colors) > 30:
raw_colors = [re.search("#.{6}", str(col)).group(0)
for col in raw_colors]
seen = set()
seen_add = seen.add
return [x for x in raw_colors if not (x in seen or seen_add(x))]
# Remove the first element because it isn't a color code.
del raw_colors[0]