a python library to draw with ASCII (but with Unicode)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.1 KiB

import math
def diagonal():
"""Draw a diagnal line from top left to bottom right"""
def f (x, y, width, height, mark, blank):
if x == math.floor((y / float(height)) * width):
return mark()
else:
return blank()
return f
# Cross
def cross ():
"""Draw two diagonals"""
def f (x, y, width, height, mark, blank):
pos = math.floor((y / float(height)) * width)
if x == pos or (width - 1) - pos == x:
return mark()
else:
return blank()
return f
def horizontal (position):
"""Draw horizontal line at given Y position."""
def f (x, y, width, height, mark, blank):
return mark() if position == y else blank()
return f
def vertical (position):
"""Draw a vertical line at given X position."""
def f (x, y, width, height, mark, blank):
return mark() if position == x else blank()
return f
# Sinus
def sinus_vertical (period=0.2, amplitude=0.5, offset_t=0, offset=0):
"""Draw a sinus shape vertically"""
period = (period / (math.pi * 2))
def f (x, y, width, height, mark, blank):
middle = (width - 1) * .5
to_mark = math.floor(middle + math.sin(offset_t + y / period) * amplitude)
return mark() if (x + offset) == to_mark else blank()
return f
def sinus_horizontal (period=0.2, amplitude=0.5, offset_t=0, offset=0):
"""Draw a sinus shape horizontally"""
period = (period / (math.pi * 2))
def f (x, y, width, height, mark, blank):
middle = (height - 1) * .5
to_mark = math.floor(middle + math.sin(offset_t + x / period) * amplitude)
return mark() if y + offset == to_mark else blank()
return f
def image (path, threshold=128):
"""Put marks based on a mask. Requires Pillow"""
try:
from PIL import Image
except ImportError:
print('ERROR: The image pattern requires Pillow to be installed.\nInstall using `pip install pillow`')
exit()
im = Image.open(path).convert('L')
image_width, image_height = im.size
pixels = im.load()
def f (x, y, width, height, mark, blank):
if x < image_width and y < image_height:
if pixels[x, y] < threshold:
return mark()
return blank()
return f