new braille method

This commit is contained in:
iou1name 2018-01-20 20:44:04 -05:00
parent fedab1a585
commit 66c9916826

View File

@ -1,5 +1,4 @@
#! /usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" """
ASCII ASCII
""" """
@ -16,7 +15,7 @@ BRAIL_CHARS = "⠿⠾⠼⠸⠰⠠ "
HEADERS = {'User-Agent': 'Gimme ascii.'} HEADERS = {'User-Agent': 'Gimme ascii.'}
def scale_image(image, maxDim=100): def scale_image(image, maxDim=(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
@ -27,14 +26,14 @@ def scale_image(image, maxDim=100):
""" """
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 <= maxDim and original_height <= maxDim: if original_width <= maxDim[0] and original_height <= maxDim[1]:
new_width, new_height = image.size new_width, new_height = image.size
elif original_width > original_height: elif original_width > original_height:
new_width = maxDim new_width = maxDim[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_height = maxDim new_height = maxDim[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)
image = image.resize((new_width, new_height)) image = image.resize((new_width, new_height))
@ -223,6 +222,58 @@ def handle_gif(imagePath, **kwargs):
numpngw.write_apng(file, np_ascii_seq) numpngw.write_apng(file, np_ascii_seq)
def brail_char(chunk, threshold):
"""
Accepts a numpy matrix and spits out a brail character.
"""
chunk = np.array_split(chunk, 3, axis=0)
chunk = np.concatenate(chunk, axis=1)
chunk = np.array_split(chunk, 6, axis=1)
dots = ""
for sub_chunk in chunk:
if np.mean(sub_chunk) < threshold:
dots += "1"
else:
dots += "0"
char = chr(int(dots, base=2)+10240)
return char
def image_to_brail(image, fontSize=(8,15)):
"""
An alternative method of generating brail ascii art.
"""
if not image:
image = open_image(kwargs["imagePath"])
if image.mode == "P":
image = image.convert(image.palette.mode)
if image.mode == "RGBA":
image = alpha_composite(image).convert("RGB")
image = image.convert("L")
matSize = (image.size[0] // fontSize[0], image.size[1] // fontSize[1])
if image.size[0] > fontSize[0]*100 or image.size[1] > fontSize[1]*100:
image = scale_image(image, (fontSize[0]*100, fontSize[1]*100))
image = image.crop((0, 0, matSize[0]*fontSize[0], matSize[1]*fontSize[1]))
threshold = np.mean(image)
grid = np.array(image)
grid = np.split(grid, matSize[1], axis=0)
grid = np.concatenate(grid, axis=1)
grid = np.split(grid, matSize[0]*matSize[1], axis=1)
for n, chunk in enumerate(grid):
char = brail_char(chunk, threshold)
grid[n] = char
grid = "".join(grid)
grid = [grid[n : n + matSize[0]] for n in range(0, len(grid), matSize[0])]
return "\n".join(grid)
if __name__=='__main__': if __name__=='__main__':
import argparse import argparse
@ -275,6 +326,11 @@ if __name__=='__main__':
"--brail", "--brail",
action="store_true", action="store_true",
help="Uses brail unicode characters instead of ascii characters.") help="Uses brail unicode characters instead of ascii characters.")
parser.add_argument(
"-B",
"--brail2",
action="store_true",
help="A better braille algorithm for a better you.")
args = parser.parse_args() args = parser.parse_args()
if args.animated: # --animated includes --image if args.animated: # --animated includes --image
@ -287,6 +343,12 @@ if __name__=='__main__':
handle_gif(**vars(args)) handle_gif(**vars(args))
exit() exit()
if args.brail2:
image = open_image(args.imagePath)
chars = image_to_brail(image)
print(chars)
exit()
image_ascii = image_to_ascii(None, **vars(args)) image_ascii = image_to_ascii(None, **vars(args))
if args.drawImage: if args.drawImage:
image = ascii_to_image(image_ascii) image = ascii_to_image(image_ascii)