how does git?

This commit is contained in:
iou1name 2017-11-22 17:39:36 -05:00
parent 3a10d780e5
commit 8af5015e66

390
ascii.py
View File

@ -1,195 +1,195 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
ASCII ASCII.
""" """
from io import BytesIO from io import BytesIO
import requests import requests
from PIL import Image, ImageFont, ImageDraw from PIL import Image, ImageFont, ImageDraw
#import imageio #import imageio
import numpy as np import numpy as np
import numpngw import numpngw
ASCII_CHARS = "$@%#*+=-:. " ASCII_CHARS = "$@%#*+=-:. "
HEADERS = {'User-Agent': 'Gimme the ascii.'} HEADERS = {'User-Agent': 'Gimme the ascii.'}
def scale_image(image, size=(100,100)): def scale_image(image, size=(100,100)):
""" """
Resizes an image while preserving the aspect ratio. Chooses the Resizes an image while preserving the aspect ratio. Chooses the
dimension to scale by based on whichever is larger, ensuring that dimension to scale by based on whichever is larger, ensuring that
neither width or height is ever larger than the accepted size tuple. neither width or height is ever larger than the accepted size tuple.
Because text characters are typically pretty close to a 1:2 rectangle, Because text characters are typically pretty close to a 1:2 rectangle,
we weight the width twice as much. we weight the width twice as much.
""" """
original_width, original_height = image.size original_width, original_height = image.size
original_width = original_width * 2 original_width = original_width * 2
if original_width > original_height: if original_width > original_height:
if original_width > size[0]: if original_width > size[0]:
new_width = size[0] new_width = size[0]
aspect_ratio = original_height/float(original_width) aspect_ratio = original_height/float(original_width)
new_height = int(aspect_ratio * new_width) new_height = int(aspect_ratio * new_width)
else: else:
new_width, new_height = image.size new_width, new_height = image.size
else: else:
if original_height > size[1]: if original_height > size[1]:
new_height = size[1] new_height = size[1]
aspect_ratio = original_width/float(original_height) aspect_ratio = original_width/float(original_height)
new_width = int(aspect_ratio * new_height) new_width = int(aspect_ratio * new_height)
else: else:
new_width, new_height = image.size new_width, new_height = image.size
image = image.resize((new_width, new_height)) image = image.resize((new_width, new_height))
return image return image
def pixels_to_chars(image, reverse=False): def pixels_to_chars(image, reverse=False):
""" """
Maps each pixel to an ascii char based on where it falls in the range Maps each pixel to an ascii char based on where it falls in the range
0-255 normalized to the length of ASCII_CHARS. 0-255 normalized to the length of ASCII_CHARS.
""" """
range_width = int(255 / len(ASCII_CHARS)) + (255 % len(ASCII_CHARS) > 0) range_width = int(255 / len(ASCII_CHARS)) + (255 % len(ASCII_CHARS) > 0)
pixels_in_image = list(image.getdata()) pixels_in_image = list(image.getdata())
pixels_to_chars = [] pixels_to_chars = []
for pixel_value in pixels_in_image: for pixel_value in pixels_in_image:
if reverse: if reverse:
index = -int(pixel_value/range_width)-1 index = -int(pixel_value/range_width)-1
else: else:
index = int(pixel_value/range_width) index = int(pixel_value/range_width)
pixels_to_chars.append(ASCII_CHARS[index]) pixels_to_chars.append(ASCII_CHARS[index])
return "".join(pixels_to_chars) return "".join(pixels_to_chars)
def open_image(imagePath): def open_image(imagePath):
""" """
Opens the image at the supplied file path in PIL. If an internet URL Opens the image at the supplied file path in PIL. If an internet URL
is supplied, it will download the image and then open it. is supplied, it will download the image and then open it.
""" """
try: try:
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)
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 as e:
return e return e
except OSError: except OSError:
return e return e
except Exception as e: except Exception as e:
return("Error opening image file: " + imagePath) return("Error opening image file: " + imagePath)
return image return image
def image_to_ascii(image, reverse=False): def image_to_ascii(image, reverse=False):
""" """
Reads an image file and converts it to ascii art. Returns a Reads an image file and converts it to ascii art. Returns a
newline-delineated string. If reverse is True, the ascii scale is newline-delineated string. If reverse is True, the ascii scale is
reversed. reversed.
""" """
image = scale_image(image) image = scale_image(image)
image = image.convert('L') # convert to grayscale image = image.convert('L') # convert to grayscale
chars = pixels_to_chars(image, reverse) chars = pixels_to_chars(image, reverse)
image_ascii = [] image_ascii = []
for index in range(0, len(chars), image.size[0]): for index in range(0, len(chars), image.size[0]):
image_ascii.append(chars[index: index + image.size[0]]) image_ascii.append(chars[index: index + image.size[0]])
image.close() image.close()
del(image) del(image)
return "\n".join(image_ascii) return "\n".join(image_ascii)
def ascii_to_image(image_ascii): 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, size, and image size non-fixed # TODO: make font type, size, and image size 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
font = ImageFont.truetype("LiberationMono-Regular.ttf", 14) font = ImageFont.truetype("LiberationMono-Regular.ttf", 14)
image = Image.new("RGB", (width, height), (255,255,255)) image = Image.new("RGB", (width, height), (255,255,255))
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
draw.text((0,0), image_ascii, (0,0,0), font=font, spacing=0) draw.text((0,0), image_ascii, (0,0,0), font=font, spacing=0)
return image return image
def handle_gif(output, reverse=False): def handle_gif(output, reverse=False):
image = open_image(args.imagePath) image = open_image(args.imagePath)
ascii_seq = [] ascii_seq = []
new_image = ascii_to_image(image_to_ascii(image, reverse)) new_image = ascii_to_image(image_to_ascii(image, reverse))
image.seek(1) image.seek(1)
while True: while True:
try: try:
im = ascii_to_image(image_to_ascii(image, reverse)) im = ascii_to_image(image_to_ascii(image, reverse))
ascii_seq.append(im) ascii_seq.append(im)
image.seek(image.tell()+1) image.seek(image.tell()+1)
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]
#images2gif.writeGif(output, ascii_seq, nq=10, subRectangles=False) #images2gif.writeGif(output, ascii_seq, nq=10, subRectangles=False)
#imageio.mimsave(output, np_ascii_seq) #imageio.mimsave(output, np_ascii_seq)
with open(output, "wb") as file: with open(output, "wb") as file:
numpngw.write_apng(file, np_ascii_seq) numpngw.write_apng(file, np_ascii_seq)
if __name__=='__main__': if __name__=='__main__':
import argparse import argparse
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
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 full path to the image file.")
parser.add_argument( parser.add_argument(
"-r", "-r",
"--reverse", "--reverse",
action="store_true", action="store_true",
help="Reverses the ascii scale.") help="Reverses the ascii scale.")
parser.add_argument( parser.add_argument(
"-o", "-o",
"--output", "--output",
help="Outputs the ascii art into a file at the specified path.") help="Outputs the ascii art into a file at the specified path.")
parser.add_argument( parser.add_argument(
"-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 plain text. \
Requires --output.") Requires --output.")
parser.add_argument( parser.add_argument(
"-a", "-a",
"--animated", "--animated",
action="store_true", action="store_true",
help="Handles animated GIFs. Includes --image.") help="Handles animated GIFs. Includes --image.")
parser.set_defaults(reverse=False, image=False, animated=False) parser.set_defaults(reverse=False, image=False, animated=False)
args = parser.parse_args() args = parser.parse_args()
if args.animated: # --animated includes --image if args.animated: # --animated includes --image
args.image = True args.image = True
if args.image: # --image requires --output if args.image: # --image requires --output
if not args.output: if not args.output:
parser.error("--image requires --output") parser.error("--image requires --output")
if args.animated: if args.animated:
handle_gif(args.output, args.reverse) handle_gif(args.output, args.reverse)
else: else:
image = open_image(args.imagePath) image = open_image(args.imagePath)
image_ascii = image_to_ascii(image, args.reverse) image_ascii = image_to_ascii(image, args.reverse)
if args.image: if args.image:
image = ascii_to_image(image_ascii) image = ascii_to_image(image_ascii)
image.save(args.output, "PNG") image.save(args.output, "PNG")
elif args.output: elif args.output:
with open(args.output, "w+") as file: with open(args.output, "w+") as file:
file.write(image_ascii) file.write(image_ascii)
else: else:
print(image_ascii) print(image_ascii)