Guest User

poo pii poo pii poo

a guest
Nov 11th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.91 KB | None | 0 0
  1. #!/usr/bin/env python3.6
  2. import argparse as ap
  3. import pyperclip as pc
  4. from os import path
  5. from PIL import Image
  6.  
  7. # POO PII POO PII POO
  8. # SirSeigneur Maitre Race
  9.  
  10. DEFAULT_ASCII = {
  11.     # layer per character
  12.     "layers": 5,
  13.  
  14.     # space between letter, bg is used
  15.     "spacing_letters": " " * 2,
  16.  
  17.     # space between new line, new line is used
  18.     "spacing_lines": 1,
  19.  
  20.     # letter to mark the foreground
  21.     "fill": "x",
  22.  
  23.     # default character
  24.     "none": [
  25.         "xxxxx",
  26.         "xxxxx",
  27.         "xxxxx",
  28.         "xxxxx",
  29.         "xxxxx",
  30.     ],
  31.  
  32.     # special
  33.  
  34.     # space
  35.     " ": [
  36.         "     ",
  37.         "     ",
  38.         "     ",
  39.         "     ",
  40.         "     ",
  41.     ],
  42.  
  43.     "-": [
  44.         "     ",
  45.         "     ",
  46.         " xxx ",
  47.         "     ",
  48.         "     ",
  49.     ],
  50.  
  51.     "+": [
  52.         "     ",
  53.         "  x  ",
  54.         " xxx ",
  55.         "  x  ",
  56.         "     ",
  57.     ],
  58.  
  59.     "=": [
  60.         "     ",
  61.         " xxx ",
  62.         "     ",
  63.         " xxx ",
  64.         "     ",
  65.     ],
  66.  
  67.     "*": [
  68.         " x x ",
  69.         "  x  ",
  70.         " x x ",
  71.         "     ",
  72.         "     ",
  73.     ],
  74.  
  75.     "`": [
  76.         "  x  ",
  77.         "   x ",
  78.         "     ",
  79.         "     ",
  80.         "     ",
  81.     ],
  82.  
  83.     "'": [
  84.         "  x  ",
  85.         "  x  ",
  86.         "     ",
  87.         "     ",
  88.         "     ",
  89.     ],
  90.  
  91.     # double-quote "
  92.     "\"": [
  93.         " x x ",
  94.         " x x ",
  95.         "     ",
  96.         "     ",
  97.         "     ",
  98.     ],
  99.  
  100.     "^": [
  101.         "  x  ",
  102.         " x x ",
  103.         "x   x",
  104.         "     ",
  105.         "     ",
  106.     ],
  107.  
  108.     "@": [
  109.         " xxx ",
  110.         "x x x",
  111.         "x xxx",
  112.         "x    ",
  113.         " xxxx",
  114.     ],
  115.  
  116.     "#": [
  117.         " x x ",
  118.         "xxxxx",
  119.         " x x ",
  120.         "xxxxx",
  121.         " x x ",
  122.     ],
  123.  
  124.     "~": [
  125.         "     ",
  126.         " xx x",
  127.         "x xx ",
  128.         "     ",
  129.         "     ",
  130.     ],
  131.  
  132.     "&": [
  133.         " xx  ",
  134.         "x  x ",
  135.         " xx x",
  136.         "x  x ",
  137.         " xx x",
  138.     ],
  139.  
  140.     "?": [
  141.         " xxx ",
  142.         "    x",
  143.         "  xx ",
  144.         "     ",
  145.         "  x  ",
  146.     ],
  147.  
  148.     "$": [
  149.         " xxx ",
  150.         "x x  ",
  151.         " xxx",
  152.         "  x x",
  153.         " xxx ",
  154.     ],
  155.  
  156.     "%": [
  157.         " x  x",
  158.         "x xx ",
  159.         "  x  ",
  160.         " xx x",
  161.         "x  x ",
  162.     ],
  163.  
  164.     ",": [
  165.         "     ",
  166.         "     ",
  167.         "     ",
  168.         "   x ",
  169.         " xx  ",
  170.     ],
  171.  
  172.     ".": [
  173.         "     ",
  174.         "     ",
  175.         "     ",
  176.         "     ",
  177.         " x   ",
  178.     ],
  179.  
  180.     ";": [
  181.         "     ",
  182.         "     ",
  183.         " x   ",
  184.         "   x ",
  185.         " xx  ",
  186.     ],
  187.  
  188.     ":": [
  189.         "     ",
  190.         "  x  ",
  191.         "     ",
  192.         "  x  ",
  193.         "     ",
  194.     ],
  195.  
  196.     "!": [
  197.         "  x  ",
  198.         "  x  ",
  199.         "  x  ",
  200.         "     ",
  201.         "  x  ",
  202.     ],
  203.  
  204.  
  205.     "(": [
  206.         "  x  ",
  207.         " x   ",
  208.         " x   ",
  209.         " x   ",
  210.         "  x  ",
  211.     ],
  212.  
  213.     ")": [
  214.         "  x  ",
  215.         "   x ",
  216.         "   x ",
  217.         "   x ",
  218.         "  x  ",
  219.     ],
  220.  
  221.     "[": [
  222.         " xxx ",
  223.         " x   ",
  224.         " x   ",
  225.         " x   ",
  226.         " xxx ",
  227.     ],
  228.  
  229.     "]": [
  230.         " xxx ",
  231.         "   x ",
  232.         "   x ",
  233.         "   x ",
  234.         " xxx ",
  235.     ],
  236.  
  237.  
  238.     "{": [
  239.         "   x ",
  240.         "  x  ",
  241.         " xx  ",
  242.         "  x  ",
  243.         "   x ",
  244.     ],
  245.  
  246.     "}": [
  247.         " x   ",
  248.         "  x  ",
  249.         "  xx ",
  250.         "  x  ",
  251.         " x   ",
  252.     ],
  253.  
  254.     "<": [
  255.         "   xx",
  256.         " xx  ",
  257.         "x    ",
  258.         " xx  ",
  259.         "   xx",
  260.     ],
  261.  
  262.     ">": [
  263.         "xx   ",
  264.         "  xx ",
  265.         "    x",
  266.         "  xx ",
  267.         "xx   ",
  268.     ],
  269.  
  270.     "|": [
  271.         "  x  ",
  272.         "  x  ",
  273.         "  x  ",
  274.         "  x  ",
  275.         "  x  ",
  276.     ],
  277.  
  278.     "_": [
  279.         "     ",
  280.         "     ",
  281.         "     ",
  282.         "     ",
  283.         "xxxxx",
  284.     ],
  285.  
  286.     # bakc-slash \
  287.     "\\": [
  288.         "x    ",
  289.         "xx   ",
  290.         " xxx ",
  291.         "   xx",
  292.         "    x",
  293.     ],
  294.  
  295.     "/": [
  296.         "    x",
  297.         "   xx",
  298.         " xxx ",
  299.         "xx   ",
  300.         "x    ",
  301.     ],
  302.  
  303.     # numbers
  304.  
  305.     "0": [
  306.         " xxx ",
  307.         "x  xx",
  308.         "x x x",
  309.         "xx  x",
  310.         " xxx ",
  311.     ],
  312.  
  313.     "1": [
  314.         "  x  ",
  315.         " xx  ",
  316.         "  x  ",
  317.         "  x  ",
  318.         " xxx ",
  319.     ],
  320.  
  321.     "2": [
  322.         "xxxx ",
  323.         "    x",
  324.         " xxx ",
  325.         "x    ",
  326.         "xxxxx",
  327.     ],
  328.  
  329.     "3": [
  330.         "xxxxx",
  331.         "    x",
  332.         " xxx ",
  333.         "    x",
  334.         "xxxx ",
  335.     ],
  336.  
  337.     "4": [
  338.         "  xx ",
  339.         " x x ",
  340.         "x  x ",
  341.         "xxxxx",
  342.         "   x ",
  343.     ],
  344.  
  345.     "5": [
  346.         "xxxxx",
  347.         "x    ",
  348.         "xxxx ",
  349.         "    x",
  350.         "xxxx ",
  351.     ],
  352.  
  353.     "6": [
  354.         " xxxx",
  355.         "x    ",
  356.         "xxxx ",
  357.         "x   x",
  358.         " xxx ",
  359.     ],
  360.  
  361.     "7": [
  362.         "xxxxx",
  363.         "    x",
  364.         " xxxx",
  365.         "  x  ",
  366.         " x   ",
  367.     ],
  368.  
  369.     "8": [
  370.         " xxx ",
  371.         "x   x",
  372.         " xxx ",
  373.         "x   x",
  374.         " xxx ",
  375.     ],
  376.  
  377.     "9": [
  378.         " xxx ",
  379.         "x   x",
  380.         " xxxx",
  381.         "    x",
  382.         " xxx ",
  383.     ],
  384.  
  385.     # extra non-ascii
  386.  
  387.     "°": [
  388.         "  x  ",
  389.         " x x ",
  390.         "  x  ",
  391.         "     ",
  392.         "     ",
  393.     ],
  394.  
  395.     "¨": [
  396.         " x x ",
  397.         "     ",
  398.         "     ",
  399.         "     ",
  400.         "     ",
  401.     ],
  402.  
  403.     # tab, 4 space
  404.     "\t": [
  405.         "                    ",
  406.         "                    ",
  407.         "                    ",
  408.         "                    ",
  409.         "                    ",
  410.     ],
  411.  
  412.     "§": [
  413.         " xxxx",
  414.         "x x  ",
  415.         " x x ",
  416.         "  x x",
  417.         "xxxx ",
  418.     ],
  419.  
  420.     "µ": [
  421.         "x   x",
  422.         "x   x",
  423.         "x   x",
  424.         "xxxx ",
  425.         "x    ",
  426.     ],
  427.  
  428.     # letter upper
  429.  
  430.     "A": [
  431.         " xxx ",
  432.         "x   x",
  433.         "xxxxx",
  434.         "x   x",
  435.         "x   x",
  436.     ],
  437.  
  438.     "B": [
  439.         "xxxx ",
  440.         "x   x",
  441.         "xxxx ",
  442.         "x   x",
  443.         "xxxxx",
  444.     ],
  445.  
  446.     "C": [
  447.         " xxxx",
  448.         "x    ",
  449.         "x    ",
  450.         "x    ",
  451.         " xxxx",
  452.     ],
  453.  
  454.     "D": [
  455.         "xxxx ",
  456.         "x   x",
  457.         "x   x",
  458.         "x   x",
  459.         "xxxx ",
  460.     ],
  461.  
  462.     "E": [
  463.         " xxxx",
  464.         "x    ",
  465.         "xxxx ",
  466.         "x    ",
  467.         " xxxx",
  468.     ],
  469.  
  470.     "F": [
  471.         " xxxx",
  472.         "x    ",
  473.         "xxxx ",
  474.         "x    ",
  475.         "x    ",
  476.     ],
  477.  
  478.     "G": [
  479.         " xxxx",
  480.         "x    ",
  481.         "x  xx",
  482.         "x   x",
  483.         " xxx ",
  484.     ],
  485.  
  486.     "H": [
  487.         "x   x",
  488.         "x   x",
  489.         "xxxxx",
  490.         "x   x",
  491.         "x   x",
  492.     ],
  493.  
  494.     "I": [
  495.         " xxx ",
  496.         "  x  ",
  497.         "  x  ",
  498.         "  x  ",
  499.         " xxx ",
  500.     ],
  501.  
  502.     "J": [
  503.         "xxxxx",
  504.         "    x",
  505.         "    x",
  506.         "x   x",
  507.         " xxx ",
  508.     ],
  509.  
  510.     "K": [
  511.         "x   x",
  512.         "x xx ",
  513.         "xx   ",
  514.         "x xx ",
  515.         "x   x",
  516.     ],
  517.  
  518.     "L": [
  519.         "x    ",
  520.         "x    ",
  521.         "x    ",
  522.         "x    ",
  523.         "xxxxx",
  524.     ],
  525.  
  526.     "M": [
  527.         "x   x",
  528.         "xx xx",
  529.         "x x x",
  530.         "x   x",
  531.         "x   x",
  532.     ],
  533.  
  534.     "N": [
  535.         "x   x",
  536.         "xx  x",
  537.         "x x x",
  538.         "x  xx",
  539.         "x   x",
  540.     ],
  541.  
  542.     "O": [
  543.         " xxx ",
  544.         "x   x",
  545.         "x   x",
  546.         "x   x",
  547.         " xxx ",
  548.     ],
  549.  
  550.     "P": [
  551.         "xxxx ",
  552.         "x   x",
  553.         "xxxx ",
  554.         "x    ",
  555.         "x    ",
  556.     ],
  557.  
  558.     "Q": [
  559.         " xxx ",
  560.         "x   x",
  561.         "x   x",
  562.         "x  x ",
  563.         " xx x",
  564.     ],
  565.  
  566.     "R": [
  567.         "xxx  ",
  568.         "x  x ",
  569.         "xxx  ",
  570.         "x  x ",
  571.         "x   x",
  572.     ],
  573.  
  574.     "S": [
  575.         " xxxx",
  576.         "x    ",
  577.         " xxx ",
  578.         "    x",
  579.         "xxxx ",
  580.     ],
  581.  
  582.     "T": [
  583.         "xxxxx",
  584.         "  x  ",
  585.         "  x  ",
  586.         "  x  ",
  587.         "  x  ",
  588.     ],
  589.  
  590.     "U": [
  591.         "x   x",
  592.         "x   x",
  593.         "x   x",
  594.         "x   x",
  595.         " xxx ",
  596.     ],
  597.  
  598.     "V": [
  599.         "x   x",
  600.         "x   x",
  601.         " x x ",
  602.         " x x ",
  603.         "  x  ",
  604.     ],
  605.  
  606.     "W": [
  607.         "x   x",
  608.         "x   x",
  609.         "x x x",
  610.         "x x x",
  611.         " x x ",
  612.     ],
  613.  
  614.     "X": [
  615.         "x   x",
  616.         " x x ",
  617.         "  x ",
  618.         " x x ",
  619.         "x   x",
  620.     ],
  621.  
  622.     "Y": [
  623.         "x   x",
  624.         " x x ",
  625.         "  x  ",
  626.         "  x  ",
  627.         "  x  ",
  628.     ],
  629.  
  630.     "Z": [
  631.         "xxxxx",
  632.         "    x",
  633.         " xxx ",
  634.         "x    ",
  635.         "xxxxx",
  636.     ],
  637.  
  638.     # letter lower
  639.  
  640.     "a": [
  641.         "     ",
  642.         " xxx ",
  643.         "x   x",
  644.         "x  xx",
  645.         " xx x",
  646.     ],
  647.  
  648.     "b": [
  649.         "x    ",
  650.         "x    ",
  651.         "xxxx ",
  652.         "x   x",
  653.         "xxxx ",
  654.     ],
  655.  
  656.     "c": [
  657.         "     ",
  658.         " xxx ",
  659.         "x    ",
  660.         "x    ",
  661.         " xxx ",
  662.     ],
  663.  
  664.  
  665.     "d": [
  666.         "    x",
  667.         "    x",
  668.         " xxxx",
  669.         "x   x",
  670.         " xxxx",
  671.     ],
  672.  
  673.     "e": [
  674.         "     ",
  675.         " xxx ",
  676.         "xxxxx",
  677.         "x    ",
  678.         " xxx ",
  679.     ],
  680.  
  681.     "f": [
  682.         "  xx ",
  683.         " x   ",
  684.         "xxxx ",
  685.         " x   ",
  686.         " x   ",
  687.     ],
  688.  
  689.     "g": [
  690.         "     ",
  691.         " xxx ",
  692.         "x    ",
  693.         "x  xx",
  694.         " xxx ",
  695.     ],
  696.  
  697.     "h": [
  698.         "x    ",
  699.         "x    ",
  700.         "xxxx ",
  701.         "x   x",
  702.         "x   x",
  703.     ],
  704.  
  705.     "i": [
  706.         "  x  ",
  707.         "     ",
  708.         " xx  ",
  709.         "  x  ",
  710.         " xxx ",
  711.     ],
  712.  
  713.     "j": [
  714.         "  x  ",
  715.         " xx  ",
  716.         "  x  ",
  717.         "  x  ",
  718.         " x   ",
  719.     ],
  720.  
  721.     "k": [
  722.         "x    ",
  723.         "x  xx",
  724.         "xxx  ",
  725.         "x  x ",
  726.         "x   x",
  727.     ],
  728.  
  729.     "l": [
  730.         "x    ",
  731.         "x    ",
  732.         "x    ",
  733.         "x    ",
  734.         " xxx ",
  735.     ],
  736.  
  737.     "m": [
  738.         "     ",
  739.         " x x ",
  740.         "x x x",
  741.         "x x x",
  742.         "x x x",
  743.     ],
  744.  
  745.     "n": [
  746.         "     ",
  747.         "xxxx ",
  748.         "x   x",
  749.         "x   x",
  750.         "x   x",
  751.     ],
  752.  
  753.     "o": [
  754.         "     ",
  755.         " xxx ",
  756.         "x   x",
  757.         "x   x",
  758.         " xxx ",
  759.     ],
  760.  
  761.     "p": [
  762.         "     ",
  763.         " xxx ",
  764.         "x   x",
  765.         "xxxx ",
  766.         "x    ",
  767.     ],
  768.  
  769.     "q": [
  770.         "     ",
  771.         " xxx ",
  772.         "x   x",
  773.         " xxxx",
  774.         "    x",
  775.     ],
  776.  
  777.     "r": [
  778.         "     ",
  779.         "x xxx",
  780.         "xx   ",
  781.         "x    ",
  782.         "x    ",
  783.     ],
  784.  
  785.     "s": [
  786.         "     ",
  787.         " xxxx",
  788.         "xxx  ",
  789.         "  xxx",
  790.         "xxxx ",
  791.     ],
  792.  
  793.     "t": [
  794.         " x   ",
  795.         "xxx  ",
  796.         " x   ",
  797.         " x   ",
  798.         "  xx ",
  799.     ],
  800.  
  801.     "u": [
  802.         "     ",
  803.         "x   x",
  804.         "x   x",
  805.         "x   x",
  806.         " xxxx",
  807.     ],
  808.  
  809.     "v": [
  810.         "     ",
  811.         "x   x",
  812.         "x   x",
  813.         " x x ",
  814.         "  x  ",
  815.     ],
  816.  
  817.     "w": [
  818.         "     ",
  819.         "x   x",
  820.         "x x x",
  821.         "x x x",
  822.         " x x ",
  823.     ],
  824.  
  825.     "x": [
  826.         "     ",
  827.         "x   x",
  828.         " xxx ",
  829.         " xxx ",
  830.         "x   x",
  831.     ],
  832.  
  833.     "y": [
  834.         "     ",
  835.         "x   x",
  836.         " x x ",
  837.         "  x  ",
  838.         "  x  ",
  839.     ],
  840.  
  841.     "z": [
  842.         "     ",
  843.         "xxxxx",
  844.         "  xx ",
  845.         " xx  ",
  846.         "xxxxx",
  847.     ],
  848.  
  849. }
  850.  
  851.  
  852.  
  853. def convert_image_to_bbtext(filepath):
  854.     image = Image.open(filepath)
  855.     raw_data = list(image.getdata())
  856.     width, height = image.size
  857.     pixels_array = [raw_data[i * width: (i + 1) * width] for i in range(height)]
  858.     return convert_pixel_array_to_bbtext(pixels_array)
  859.  
  860.  
  861. def convert_pixel_array_to_bbtext(pixels_array):
  862.     text_buffer = ""
  863.  
  864.     for column in pixels_array:
  865.         last_color = None
  866.  
  867.         for pixel in column:
  868.             color = "{0:02x}{1:02x}{2:02x}".format(*pixel)
  869.  
  870.             # Optimisation/Compression for a smaller size
  871.             if color != last_color:
  872.                 text_buffer += "</color>" if last_color != None else ""
  873.                 text_buffer += f"<color=#{color}>█"
  874.                 last_color = color
  875.             else:
  876.                 text_buffer += "█"
  877.  
  878.         text_buffer += "</color>\n"
  879.  
  880.     return text_buffer
  881.  
  882.  
  883. def convert_text_to_bbtext(text, font=DEFAULT_ASCII, fg=(0, 0, 0), bg=(255, 255, 255)):
  884.     pixels_array = []
  885.  
  886.     text = bytes(text, "utf-8").decode("unicode_escape")
  887.  
  888.     for line in text.split("\n"):
  889.         for step in range(font["layers"]):
  890.             pixels_array.append([])
  891.  
  892.             for i, char in enumerate(line):
  893.                 points = font.get(char, font["none"])[step]
  894.  
  895.                 if i != len(line):
  896.                     points += font["spacing_letters"]
  897.  
  898.                 pixels_array[-1] += [fg if p == font["fill"] else bg for p in points]
  899.  
  900.         pixels_array += [[bg] * font["spacing_lines"]]
  901.  
  902.     return convert_pixel_array_to_bbtext(pixels_array)
  903.  
  904.  
  905. def interactive_input_text_convert_copy():
  906.     while True:
  907.         input_text = input("Write text to convert:> ")
  908.         converted_text = convert_text_to_bbtext(input_text)
  909.         pc.copy(converted_text)
  910.         print(f"text '{input_text[:30]}'{'...' if len(input_text) > 30 else ''}"
  911.               + " was converted and copied to the clipboard")
  912.  
  913.  
  914. def main():
  915.     parser = ap.ArgumentParser()
  916.     parser.add_argument("-f", "--file", type=str)
  917.     parser.add_argument("-o", "--output", type=str)
  918.     parser.add_argument("-v", "--verbose", action="store_true")
  919.     parser.add_argument("-c", "--copy", action="store_true")
  920.     parser.add_argument("-i", "--interactive", action="store_true")
  921.     parser.add_argument("-t", "--text", type=str)
  922.  
  923.     args = parser.parse_args()
  924.  
  925.     if args.text or args.file and not args.interactive:
  926.         if args.file and not args.text:
  927.             text = convert_image_to_bbtext(args.file)
  928.         elif args.text and not args.file:
  929.             text = convert_text_to_bbtext(args.text)
  930.         else:
  931.             print("cannot use flag file and text at the same time")
  932.             parser.print_usage()
  933.             return
  934.  
  935.         if args.output:
  936.             with open(args.output, "w") as file:
  937.                 file.write(text)
  938.  
  939.         if args.verbose:
  940.             print(text)
  941.  
  942.         if args.copy:
  943.             pc.copy(text)
  944.  
  945.     elif args.interactive:
  946.         print("entering the interactive shell")
  947.         interactive_input_text_convert_copy()
  948.     else:
  949.         parser.print_usage()
  950.  
  951. if __name__ == '__main__':
  952.     main()
Advertisement
Add Comment
Please, Sign In to add comment