added convenince args and comments

This commit is contained in:
iou1name 2017-11-30 00:04:45 -05:00
parent 50c780969d
commit e3aef08ee9

View File

@ -83,10 +83,10 @@ def char_color(pixel, code="irc"):
"light blue": (65,105,225), "pink":(255,192,203), "grey": (128,128,128),
"silver": (192,192,192)}
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3", "red": "4",
"brown": "5", "purple": "6", "orange": "7", "yellow": "8", "light green": "9",
"teal": "10", "cyan": "11", "light blue": "12", "pink": "13", "grey": "14",
"silver": "15"}
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3",
"red": "4", "brown": "5", "purple": "6", "orange": "7", "yellow": "8",
"light green": "9", "teal": "10", "cyan": "11", "light blue": "12",
"pink": "13", "grey": "14", "silver": "15"}
colors_ansi = {"white": "[1;37m", "black": "[0;30m", "blue": "[0;34m",
"green": "[0;32m", "red": "[0;31m", "brown": "[0;33m",
@ -118,11 +118,13 @@ def open_image(imagePath):
if imagePath.startswith("http"):
res = requests.get(imagePath, headers=HEADERS, verify=True,
timeout=20)
if res.status_code == 404:
return "404: file not found."
res.raise_for_status()
image = Image.open(BytesIO(res.content))
else:
image = Image.open(imagePath)
except FileNotFoundError as e:
except FileNotFoundError:
return f"File not found: {imagePath}"
except Exception as e:
return(f"Error opening image: {imagePath}\n{e}")
@ -132,20 +134,24 @@ def open_image(imagePath):
def colorize(chars, image, code):
"""
Colorizes the ascii matrix.
Colorizes the ascii matrix. Moves iteratively through the matrix and
calls char_color on each pixel/character. Spaces are skipped to save
time and CPU cycles.
"""
prefix = {"irc": "\03", "ansi":"\033"}
chars = chars.split("\n")
for j in range(0, image.size[1]):
new_row = ""
for k in range(0, image.size[0]):
if chars[j][k] == " ":
continue
new_row += prefix[code] + char_color(image.getpixel((k,j)), code)
new_row += chars[j][k]
chars[j] = new_row
chars = "\n".join(chars)
if code == "ansi":
chars += "\033[0m"
chars += "\033[0m" # to avoid lingering effects in terminals
return chars
@ -173,7 +179,7 @@ def ascii_to_image(image_ascii):
"""
Creates a plain image and draws text on it.
"""
# TODO: make font type and size non-fixed
# TODO: make font type, size and color non-fixed
width = len(image_ascii[:image_ascii.index("\n")]) * 8
height = (image_ascii.count("\n")+1) * 12 + 4
@ -200,7 +206,7 @@ def handle_gif(image, output, reverse=False):
except EOFError:
break # end of sequence
#new_image.save(output, save_all=True, append_images=ascii_seq,
# new_image.save(output, save_all=True, append_images=ascii_seq,
# duration=60, loop=0, optimize=True)
ascii_seq = [new_image] + ascii_seq
np_ascii_seq = [np.array(im) for im in ascii_seq]
@ -215,7 +221,8 @@ if __name__=='__main__':
description="Converts an image file to ascii art.")
parser.add_argument(
"imagePath",
help="The full path to the image file.")
help="The path to the image file. May be a local path or internet \
internet URL.")
parser.add_argument(
"-r",
"--reverse",
@ -229,8 +236,8 @@ if __name__=='__main__':
"-i",
"--image",
action="store_true",
help="Outputs the ascii art as an image rather than plain text. \
Requires --output.")
help="Outputs the ascii art as an image rather than text. Requires \
--output.")
parser.add_argument(
"-a",
"--animated",
@ -240,8 +247,20 @@ if __name__=='__main__':
"-c",
"--color",
type=str,
help="Colorizes the ascii matrix.")
parser.set_defaults(reverse=False, image=False, animated=False)
help="Colorizes the ascii matrix. Currently supported modes are 'irc' \
and 'ansi'.")
parser.add_argument(
"--ansi",
dest="color",
action="store_const",
const="ansi",
help="Shortcut for '--color ansi'.")
parser.add_argument(
"--irc",
dest="color",
action="store_const",
const="irc",
help="Shortcut for '--color irc'.")
args = parser.parse_args()
if args.animated: # --animated includes --image