mirror of
https://github.com/dylanaraps/pywal.git
synced 2025-02-18 03:01:03 +01:00
General: Add error handling to palette generation
This commit is contained in:
parent
b831e17555
commit
9d061fc083
31
wal.py
31
wal.py
@ -77,18 +77,36 @@ def get_image(img):
|
|||||||
return rand_img
|
return rand_img
|
||||||
|
|
||||||
|
|
||||||
|
def magic(color_count, img):
|
||||||
|
"""Call Imagemagick to generate a scheme."""
|
||||||
|
colors = subprocess.Popen(["convert", img, "+dither", "-colors",
|
||||||
|
str(color_count), "-unique-colors", "txt:-"],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE)
|
||||||
|
|
||||||
|
return colors.stdout
|
||||||
|
|
||||||
|
|
||||||
def gen_colors(img):
|
def gen_colors(img):
|
||||||
"""Generate a color palette using imagemagick."""
|
"""Generate a color palette using imagemagick."""
|
||||||
colors = []
|
colors = []
|
||||||
|
|
||||||
# Long-ass imagemagick command.
|
# Generate initial scheme.
|
||||||
magic = subprocess.Popen(["convert", img, "+dither", "-colors",
|
magic_output = magic(COLOR_COUNT, img).readlines()
|
||||||
str(COLOR_COUNT), "-unique-colors", "txt:-"],
|
|
||||||
stdout=subprocess.PIPE,
|
# If imagemagick finds less than 16 colors, use a larger source number
|
||||||
stderr=subprocess.PIPE)
|
# of colors.
|
||||||
|
index = 0
|
||||||
|
while len(magic_output) - 1 <= 15:
|
||||||
|
index += 1
|
||||||
|
magic_output = magic(COLOR_COUNT + index, img).readlines()
|
||||||
|
|
||||||
|
print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
|
||||||
|
"color palette, trying a larger palette size",
|
||||||
|
COLOR_COUNT + index)
|
||||||
|
|
||||||
# Create a list of hex colors.
|
# Create a list of hex colors.
|
||||||
for color in magic.stdout:
|
for color in magic_output:
|
||||||
hex_color = re.search('#.{6}', str(color))
|
hex_color = re.search('#.{6}', str(color))
|
||||||
|
|
||||||
if hex_color:
|
if hex_color:
|
||||||
@ -156,6 +174,7 @@ def send_sequences(colors):
|
|||||||
# Decode the string.
|
# Decode the string.
|
||||||
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
|
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
|
||||||
|
|
||||||
|
# Send the sequences to all open terminals.
|
||||||
for term in glob.glob("/dev/pts/[0-9]*"):
|
for term in glob.glob("/dev/pts/[0-9]*"):
|
||||||
term_file = open(term, 'w')
|
term_file = open(term, 'w')
|
||||||
term_file.write(sequences)
|
term_file.write(sequences)
|
||||||
|
Loading…
Reference in New Issue
Block a user