added convenince args and comments
This commit is contained in:
parent
50c780969d
commit
e3aef08ee9
49
ascii.py
49
ascii.py
|
@ -83,10 +83,10 @@ def char_color(pixel, code="irc"):
|
||||||
"light blue": (65,105,225), "pink":(255,192,203), "grey": (128,128,128),
|
"light blue": (65,105,225), "pink":(255,192,203), "grey": (128,128,128),
|
||||||
"silver": (192,192,192)}
|
"silver": (192,192,192)}
|
||||||
|
|
||||||
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3", "red": "4",
|
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3",
|
||||||
"brown": "5", "purple": "6", "orange": "7", "yellow": "8", "light green": "9",
|
"red": "4", "brown": "5", "purple": "6", "orange": "7", "yellow": "8",
|
||||||
"teal": "10", "cyan": "11", "light blue": "12", "pink": "13", "grey": "14",
|
"light green": "9", "teal": "10", "cyan": "11", "light blue": "12",
|
||||||
"silver": "15"}
|
"pink": "13", "grey": "14", "silver": "15"}
|
||||||
|
|
||||||
colors_ansi = {"white": "[1;37m", "black": "[0;30m", "blue": "[0;34m",
|
colors_ansi = {"white": "[1;37m", "black": "[0;30m", "blue": "[0;34m",
|
||||||
"green": "[0;32m", "red": "[0;31m", "brown": "[0;33m",
|
"green": "[0;32m", "red": "[0;31m", "brown": "[0;33m",
|
||||||
|
@ -118,11 +118,13 @@ def open_image(imagePath):
|
||||||
if imagePath.startswith("http"):
|
if imagePath.startswith("http"):
|
||||||
res = requests.get(imagePath, headers=HEADERS, verify=True,
|
res = requests.get(imagePath, headers=HEADERS, verify=True,
|
||||||
timeout=20)
|
timeout=20)
|
||||||
|
if res.status_code == 404:
|
||||||
|
return "404: file not found."
|
||||||
res.raise_for_status()
|
res.raise_for_status()
|
||||||
image = Image.open(BytesIO(res.content))
|
image = Image.open(BytesIO(res.content))
|
||||||
else:
|
else:
|
||||||
image = Image.open(imagePath)
|
image = Image.open(imagePath)
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError:
|
||||||
return f"File not found: {imagePath}"
|
return f"File not found: {imagePath}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return(f"Error opening image: {imagePath}\n{e}")
|
return(f"Error opening image: {imagePath}\n{e}")
|
||||||
|
@ -132,20 +134,24 @@ def open_image(imagePath):
|
||||||
|
|
||||||
def colorize(chars, image, code):
|
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"}
|
prefix = {"irc": "\03", "ansi":"\033"}
|
||||||
chars = chars.split("\n")
|
chars = chars.split("\n")
|
||||||
for j in range(0, image.size[1]):
|
for j in range(0, image.size[1]):
|
||||||
new_row = ""
|
new_row = ""
|
||||||
for k in range(0, image.size[0]):
|
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 += prefix[code] + char_color(image.getpixel((k,j)), code)
|
||||||
new_row += chars[j][k]
|
new_row += chars[j][k]
|
||||||
chars[j] = new_row
|
chars[j] = new_row
|
||||||
|
|
||||||
chars = "\n".join(chars)
|
chars = "\n".join(chars)
|
||||||
if code == "ansi":
|
if code == "ansi":
|
||||||
chars += "\033[0m"
|
chars += "\033[0m" # to avoid lingering effects in terminals
|
||||||
return chars
|
return chars
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,7 +179,7 @@ def ascii_to_image(image_ascii):
|
||||||
"""
|
"""
|
||||||
Creates a plain image and draws text on it.
|
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
|
width = len(image_ascii[:image_ascii.index("\n")]) * 8
|
||||||
height = (image_ascii.count("\n")+1) * 12 + 4
|
height = (image_ascii.count("\n")+1) * 12 + 4
|
||||||
|
|
||||||
|
@ -200,8 +206,8 @@ def handle_gif(image, output, reverse=False):
|
||||||
except EOFError:
|
except EOFError:
|
||||||
break # end of sequence
|
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)
|
# duration=60, loop=0, optimize=True)
|
||||||
ascii_seq = [new_image] + ascii_seq
|
ascii_seq = [new_image] + ascii_seq
|
||||||
np_ascii_seq = [np.array(im) for im in ascii_seq]
|
np_ascii_seq = [np.array(im) for im in ascii_seq]
|
||||||
with open(output, "wb") as file:
|
with open(output, "wb") as file:
|
||||||
|
@ -215,7 +221,8 @@ if __name__=='__main__':
|
||||||
description="Converts an image file to ascii art.")
|
description="Converts an image file to ascii art.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"imagePath",
|
"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(
|
parser.add_argument(
|
||||||
"-r",
|
"-r",
|
||||||
"--reverse",
|
"--reverse",
|
||||||
|
@ -229,8 +236,8 @@ if __name__=='__main__':
|
||||||
"-i",
|
"-i",
|
||||||
"--image",
|
"--image",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Outputs the ascii art as an image rather than plain text. \
|
help="Outputs the ascii art as an image rather than text. Requires \
|
||||||
Requires --output.")
|
--output.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-a",
|
"-a",
|
||||||
"--animated",
|
"--animated",
|
||||||
|
@ -240,8 +247,20 @@ if __name__=='__main__':
|
||||||
"-c",
|
"-c",
|
||||||
"--color",
|
"--color",
|
||||||
type=str,
|
type=str,
|
||||||
help="Colorizes the ascii matrix.")
|
help="Colorizes the ascii matrix. Currently supported modes are 'irc' \
|
||||||
parser.set_defaults(reverse=False, image=False, animated=False)
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.animated: # --animated includes --image
|
if args.animated: # --animated includes --image
|
||||||
|
|
Loading…
Reference in New Issue
Block a user