Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.6
- import argparse as ap
- import pyperclip as pc
- from os import path
- from PIL import Image
- # POO PII POO PII POO
- # SirSeigneur Maitre Race
- DEFAULT_ASCII = {
- # layer per character
- "layers": 5,
- # space between letter, bg is used
- "spacing_letters": " " * 2,
- # space between new line, new line is used
- "spacing_lines": 1,
- # letter to mark the foreground
- "fill": "x",
- # default character
- "none": [
- "xxxxx",
- "xxxxx",
- "xxxxx",
- "xxxxx",
- "xxxxx",
- ],
- # special
- # space
- " ": [
- " ",
- " ",
- " ",
- " ",
- " ",
- ],
- "-": [
- " ",
- " ",
- " xxx ",
- " ",
- " ",
- ],
- "+": [
- " ",
- " x ",
- " xxx ",
- " x ",
- " ",
- ],
- "=": [
- " ",
- " xxx ",
- " ",
- " xxx ",
- " ",
- ],
- "*": [
- " x x ",
- " x ",
- " x x ",
- " ",
- " ",
- ],
- "`": [
- " x ",
- " x ",
- " ",
- " ",
- " ",
- ],
- "'": [
- " x ",
- " x ",
- " ",
- " ",
- " ",
- ],
- # double-quote "
- "\"": [
- " x x ",
- " x x ",
- " ",
- " ",
- " ",
- ],
- "^": [
- " x ",
- " x x ",
- "x x",
- " ",
- " ",
- ],
- "@": [
- " xxx ",
- "x x x",
- "x xxx",
- "x ",
- " xxxx",
- ],
- "#": [
- " x x ",
- "xxxxx",
- " x x ",
- "xxxxx",
- " x x ",
- ],
- "~": [
- " ",
- " xx x",
- "x xx ",
- " ",
- " ",
- ],
- "&": [
- " xx ",
- "x x ",
- " xx x",
- "x x ",
- " xx x",
- ],
- "?": [
- " xxx ",
- " x",
- " xx ",
- " ",
- " x ",
- ],
- "$": [
- " xxx ",
- "x x ",
- " xxx",
- " x x",
- " xxx ",
- ],
- "%": [
- " x x",
- "x xx ",
- " x ",
- " xx x",
- "x x ",
- ],
- ",": [
- " ",
- " ",
- " ",
- " x ",
- " xx ",
- ],
- ".": [
- " ",
- " ",
- " ",
- " ",
- " x ",
- ],
- ";": [
- " ",
- " ",
- " x ",
- " x ",
- " xx ",
- ],
- ":": [
- " ",
- " x ",
- " ",
- " x ",
- " ",
- ],
- "!": [
- " x ",
- " x ",
- " x ",
- " ",
- " x ",
- ],
- "(": [
- " x ",
- " x ",
- " x ",
- " x ",
- " x ",
- ],
- ")": [
- " x ",
- " x ",
- " x ",
- " x ",
- " x ",
- ],
- "[": [
- " xxx ",
- " x ",
- " x ",
- " x ",
- " xxx ",
- ],
- "]": [
- " xxx ",
- " x ",
- " x ",
- " x ",
- " xxx ",
- ],
- "{": [
- " x ",
- " x ",
- " xx ",
- " x ",
- " x ",
- ],
- "}": [
- " x ",
- " x ",
- " xx ",
- " x ",
- " x ",
- ],
- "<": [
- " xx",
- " xx ",
- "x ",
- " xx ",
- " xx",
- ],
- ">": [
- "xx ",
- " xx ",
- " x",
- " xx ",
- "xx ",
- ],
- "|": [
- " x ",
- " x ",
- " x ",
- " x ",
- " x ",
- ],
- "_": [
- " ",
- " ",
- " ",
- " ",
- "xxxxx",
- ],
- # bakc-slash \
- "\\": [
- "x ",
- "xx ",
- " xxx ",
- " xx",
- " x",
- ],
- "/": [
- " x",
- " xx",
- " xxx ",
- "xx ",
- "x ",
- ],
- # numbers
- "0": [
- " xxx ",
- "x xx",
- "x x x",
- "xx x",
- " xxx ",
- ],
- "1": [
- " x ",
- " xx ",
- " x ",
- " x ",
- " xxx ",
- ],
- "2": [
- "xxxx ",
- " x",
- " xxx ",
- "x ",
- "xxxxx",
- ],
- "3": [
- "xxxxx",
- " x",
- " xxx ",
- " x",
- "xxxx ",
- ],
- "4": [
- " xx ",
- " x x ",
- "x x ",
- "xxxxx",
- " x ",
- ],
- "5": [
- "xxxxx",
- "x ",
- "xxxx ",
- " x",
- "xxxx ",
- ],
- "6": [
- " xxxx",
- "x ",
- "xxxx ",
- "x x",
- " xxx ",
- ],
- "7": [
- "xxxxx",
- " x",
- " xxxx",
- " x ",
- " x ",
- ],
- "8": [
- " xxx ",
- "x x",
- " xxx ",
- "x x",
- " xxx ",
- ],
- "9": [
- " xxx ",
- "x x",
- " xxxx",
- " x",
- " xxx ",
- ],
- # extra non-ascii
- "°": [
- " x ",
- " x x ",
- " x ",
- " ",
- " ",
- ],
- "¨": [
- " x x ",
- " ",
- " ",
- " ",
- " ",
- ],
- # tab, 4 space
- "\t": [
- " ",
- " ",
- " ",
- " ",
- " ",
- ],
- "§": [
- " xxxx",
- "x x ",
- " x x ",
- " x x",
- "xxxx ",
- ],
- "µ": [
- "x x",
- "x x",
- "x x",
- "xxxx ",
- "x ",
- ],
- # letter upper
- "A": [
- " xxx ",
- "x x",
- "xxxxx",
- "x x",
- "x x",
- ],
- "B": [
- "xxxx ",
- "x x",
- "xxxx ",
- "x x",
- "xxxxx",
- ],
- "C": [
- " xxxx",
- "x ",
- "x ",
- "x ",
- " xxxx",
- ],
- "D": [
- "xxxx ",
- "x x",
- "x x",
- "x x",
- "xxxx ",
- ],
- "E": [
- " xxxx",
- "x ",
- "xxxx ",
- "x ",
- " xxxx",
- ],
- "F": [
- " xxxx",
- "x ",
- "xxxx ",
- "x ",
- "x ",
- ],
- "G": [
- " xxxx",
- "x ",
- "x xx",
- "x x",
- " xxx ",
- ],
- "H": [
- "x x",
- "x x",
- "xxxxx",
- "x x",
- "x x",
- ],
- "I": [
- " xxx ",
- " x ",
- " x ",
- " x ",
- " xxx ",
- ],
- "J": [
- "xxxxx",
- " x",
- " x",
- "x x",
- " xxx ",
- ],
- "K": [
- "x x",
- "x xx ",
- "xx ",
- "x xx ",
- "x x",
- ],
- "L": [
- "x ",
- "x ",
- "x ",
- "x ",
- "xxxxx",
- ],
- "M": [
- "x x",
- "xx xx",
- "x x x",
- "x x",
- "x x",
- ],
- "N": [
- "x x",
- "xx x",
- "x x x",
- "x xx",
- "x x",
- ],
- "O": [
- " xxx ",
- "x x",
- "x x",
- "x x",
- " xxx ",
- ],
- "P": [
- "xxxx ",
- "x x",
- "xxxx ",
- "x ",
- "x ",
- ],
- "Q": [
- " xxx ",
- "x x",
- "x x",
- "x x ",
- " xx x",
- ],
- "R": [
- "xxx ",
- "x x ",
- "xxx ",
- "x x ",
- "x x",
- ],
- "S": [
- " xxxx",
- "x ",
- " xxx ",
- " x",
- "xxxx ",
- ],
- "T": [
- "xxxxx",
- " x ",
- " x ",
- " x ",
- " x ",
- ],
- "U": [
- "x x",
- "x x",
- "x x",
- "x x",
- " xxx ",
- ],
- "V": [
- "x x",
- "x x",
- " x x ",
- " x x ",
- " x ",
- ],
- "W": [
- "x x",
- "x x",
- "x x x",
- "x x x",
- " x x ",
- ],
- "X": [
- "x x",
- " x x ",
- " x ",
- " x x ",
- "x x",
- ],
- "Y": [
- "x x",
- " x x ",
- " x ",
- " x ",
- " x ",
- ],
- "Z": [
- "xxxxx",
- " x",
- " xxx ",
- "x ",
- "xxxxx",
- ],
- # letter lower
- "a": [
- " ",
- " xxx ",
- "x x",
- "x xx",
- " xx x",
- ],
- "b": [
- "x ",
- "x ",
- "xxxx ",
- "x x",
- "xxxx ",
- ],
- "c": [
- " ",
- " xxx ",
- "x ",
- "x ",
- " xxx ",
- ],
- "d": [
- " x",
- " x",
- " xxxx",
- "x x",
- " xxxx",
- ],
- "e": [
- " ",
- " xxx ",
- "xxxxx",
- "x ",
- " xxx ",
- ],
- "f": [
- " xx ",
- " x ",
- "xxxx ",
- " x ",
- " x ",
- ],
- "g": [
- " ",
- " xxx ",
- "x ",
- "x xx",
- " xxx ",
- ],
- "h": [
- "x ",
- "x ",
- "xxxx ",
- "x x",
- "x x",
- ],
- "i": [
- " x ",
- " ",
- " xx ",
- " x ",
- " xxx ",
- ],
- "j": [
- " x ",
- " xx ",
- " x ",
- " x ",
- " x ",
- ],
- "k": [
- "x ",
- "x xx",
- "xxx ",
- "x x ",
- "x x",
- ],
- "l": [
- "x ",
- "x ",
- "x ",
- "x ",
- " xxx ",
- ],
- "m": [
- " ",
- " x x ",
- "x x x",
- "x x x",
- "x x x",
- ],
- "n": [
- " ",
- "xxxx ",
- "x x",
- "x x",
- "x x",
- ],
- "o": [
- " ",
- " xxx ",
- "x x",
- "x x",
- " xxx ",
- ],
- "p": [
- " ",
- " xxx ",
- "x x",
- "xxxx ",
- "x ",
- ],
- "q": [
- " ",
- " xxx ",
- "x x",
- " xxxx",
- " x",
- ],
- "r": [
- " ",
- "x xxx",
- "xx ",
- "x ",
- "x ",
- ],
- "s": [
- " ",
- " xxxx",
- "xxx ",
- " xxx",
- "xxxx ",
- ],
- "t": [
- " x ",
- "xxx ",
- " x ",
- " x ",
- " xx ",
- ],
- "u": [
- " ",
- "x x",
- "x x",
- "x x",
- " xxxx",
- ],
- "v": [
- " ",
- "x x",
- "x x",
- " x x ",
- " x ",
- ],
- "w": [
- " ",
- "x x",
- "x x x",
- "x x x",
- " x x ",
- ],
- "x": [
- " ",
- "x x",
- " xxx ",
- " xxx ",
- "x x",
- ],
- "y": [
- " ",
- "x x",
- " x x ",
- " x ",
- " x ",
- ],
- "z": [
- " ",
- "xxxxx",
- " xx ",
- " xx ",
- "xxxxx",
- ],
- }
- def convert_image_to_bbtext(filepath):
- image = Image.open(filepath)
- raw_data = list(image.getdata())
- width, height = image.size
- pixels_array = [raw_data[i * width: (i + 1) * width] for i in range(height)]
- return convert_pixel_array_to_bbtext(pixels_array)
- def convert_pixel_array_to_bbtext(pixels_array):
- text_buffer = ""
- for column in pixels_array:
- last_color = None
- for pixel in column:
- color = "{0:02x}{1:02x}{2:02x}".format(*pixel)
- # Optimisation/Compression for a smaller size
- if color != last_color:
- text_buffer += "</color>" if last_color != None else ""
- text_buffer += f"<color=#{color}>█"
- last_color = color
- else:
- text_buffer += "█"
- text_buffer += "</color>\n"
- return text_buffer
- def convert_text_to_bbtext(text, font=DEFAULT_ASCII, fg=(0, 0, 0), bg=(255, 255, 255)):
- pixels_array = []
- text = bytes(text, "utf-8").decode("unicode_escape")
- for line in text.split("\n"):
- for step in range(font["layers"]):
- pixels_array.append([])
- for i, char in enumerate(line):
- points = font.get(char, font["none"])[step]
- if i != len(line):
- points += font["spacing_letters"]
- pixels_array[-1] += [fg if p == font["fill"] else bg for p in points]
- pixels_array += [[bg] * font["spacing_lines"]]
- return convert_pixel_array_to_bbtext(pixels_array)
- def interactive_input_text_convert_copy():
- while True:
- input_text = input("Write text to convert:> ")
- converted_text = convert_text_to_bbtext(input_text)
- pc.copy(converted_text)
- print(f"text '{input_text[:30]}'{'...' if len(input_text) > 30 else ''}"
- + " was converted and copied to the clipboard")
- def main():
- parser = ap.ArgumentParser()
- parser.add_argument("-f", "--file", type=str)
- parser.add_argument("-o", "--output", type=str)
- parser.add_argument("-v", "--verbose", action="store_true")
- parser.add_argument("-c", "--copy", action="store_true")
- parser.add_argument("-i", "--interactive", action="store_true")
- parser.add_argument("-t", "--text", type=str)
- args = parser.parse_args()
- if args.text or args.file and not args.interactive:
- if args.file and not args.text:
- text = convert_image_to_bbtext(args.file)
- elif args.text and not args.file:
- text = convert_text_to_bbtext(args.text)
- else:
- print("cannot use flag file and text at the same time")
- parser.print_usage()
- return
- if args.output:
- with open(args.output, "w") as file:
- file.write(text)
- if args.verbose:
- print(text)
- if args.copy:
- pc.copy(text)
- elif args.interactive:
- print("entering the interactive shell")
- interactive_input_text_convert_copy()
- else:
- parser.print_usage()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment