archive- Random tools & helpful resources for IRC |
git clone git://git.acid.vegas/archive.git |
Log | Files | Refs | Archive |
ascii2png.py (5955B)
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Scroll IRC Art Bot - Developed by acidvegas in Python (https://acid.vegas/scroll) 4 # ascii2png.py 5 6 ''' 7 Credits to VXP for making the original "pngbot" script (https://github.com/lalbornoz/MiRCARTools) 8 ''' 9 10 import os 11 import urllib.request 12 13 from PIL import Image, ImageDraw, ImageFont 14 15 def flip_cell_state(cellState, bit): 16 if cellState & bit: 17 return cellState & ~bit 18 else: 19 return cellState | bit 20 21 def parse_char(colourSpec, curColours): 22 if len(colourSpec) > 0: 23 colourSpec = colourSpec.split(',') 24 if len(colourSpec) == 2 and len(colourSpec[1]) > 0: 25 return (int(colourSpec[0] or curColours[0]), int(colourSpec[1])) 26 elif len(colourSpec) == 1 or len(colourSpec[1]) == 0: 27 return (int(colourSpec[0]), curColours[1]) 28 else: 29 return (15, 1) 30 31 def ascii_png(url): 32 text_file = os.path.join('data','temp.txt') 33 if os.path.isfile(text_file): 34 os.remove(text_file) 35 urllib.request.urlretrieve(url, text_file) 36 data = open(text_file) 37 inCurColourSpec = '' 38 inCurRow = -1 39 inLine = data.readline() 40 inSize = [0, 0] 41 inMaxCols = 0 42 outMap = [] 43 while inLine: 44 inCellState = 0x00 45 inParseState = 1 46 inCurCol = 0 47 inMaxCol = len(inLine) 48 inCurColourDigits = 0 49 inCurColours = (15, 1) 50 inCurColourSpec = '' 51 inCurRow += 1 52 outMap.append([]) 53 inRowCols = 0 54 inSize[1] += 1 55 while inCurCol < inMaxCol: 56 inChar = inLine[inCurCol] 57 if inChar in set('\r\n'): 58 inCurCol += 1 59 elif inParseState == 1: 60 inCurCol += 1 61 if inChar == '': 62 inCellState = flip_cell_state(inCellState, 0x01) 63 elif inChar == '': 64 inParseState = 2 65 elif inChar == '': 66 inCellState = flip_cell_state(inCellState, 0x02) 67 elif inChar == '': 68 inCellState |= 0x00 69 inCurColours = (15, 1) 70 elif inChar == '': 71 inCurColours = (inCurColours[1], inCurColours[0]) 72 elif inChar == '': 73 inCellState = flip_cell_state(inCellState, 0x04) 74 else: 75 inRowCols += 1 76 outMap[inCurRow].append([*inCurColours, inCellState, inChar]) 77 elif inParseState == 2 or inParseState == 3: 78 if inChar == ',' and inParseState == 2: 79 if (inCurCol + 1) < inMaxCol and not inLine[inCurCol + 1] in set('0123456789'): 80 inCurColours = parse_char(inCurColourSpec, inCurColours) 81 inCurColourDigits = 0 82 inCurColourSpec = '' 83 inParseState = 1 84 else: 85 inCurCol += 1 86 inCurColourDigits = 0 87 inCurColourSpec += inChar 88 inParseState = 3 89 elif inChar in set('0123456789') and inCurColourDigits == 0: 90 inCurCol += 1 91 inCurColourDigits += 1 92 inCurColourSpec += inChar 93 elif inChar in set('0123456789') and inCurColourDigits == 1 and inCurColourSpec[-1] == '0': 94 inCurCol += 1 95 inCurColourDigits += 1 96 inCurColourSpec += inChar 97 elif inChar in set('012345') and inCurColourDigits == 1 and inCurColourSpec[-1] == '1': 98 inCurCol += 1 99 inCurColourDigits += 1 100 inCurColourSpec += inChar 101 else: 102 inCurColours = parse_char(inCurColourSpec, inCurColours) 103 inCurColourDigits = 0 104 inCurColourSpec = '' 105 inParseState = 1 106 inMaxCols = max(inMaxCols, inRowCols) 107 inLine = data.readline() 108 inSize[0] = inMaxCols 109 canvas_data = outMap 110 numRowCols = 0 111 for numRow in range(len(outMap)): 112 numRowCols = max(numRowCols, len(outMap[numRow])) 113 for numRow in range(len(outMap)): 114 if len(outMap[numRow]) != numRowCols: 115 for numColOff in range(numRowCols - len(outMap[numRow])): 116 outMap[numRow].append([1,1,0,' ']) 117 outMap[numRow].insert(0,[1,1,0,' ']) 118 outMap[numRow].append([1,1,0,' ']) 119 outMap.insert(0,[[1,1,0,' ']] * len(outMap[0])) 120 outMap.append([[1,1,0,' ']] * len(outMap[0])) 121 inCanvasMap = outMap 122 outImgFont = ImageFont.truetype(os.path.join('data','DejaVuSansMono.ttf'), 11) 123 outImgFontSize = [*outImgFont.getsize(' ')] 124 outImgFontSize[1] += 3 125 ColorsBold = [[255,255,255],[85,85,85],[85,85,255],[85,255,85],[255,85,85],[255,85,85],[255,85,255],[255,255,85],[255,255,85],[85,255,85],[85,255,255],[85,255,255],[85,85,255],[255,85,255],[85,85,85],[255,255,255]] 126 ColorsNormal = [[255,255,255],[0,0,0],[0,0,187],[0,187,0],[255,85,85],[187,0,0],[187,0,187],[187,187,0],[255,255,85],[85,255,85],[0,187,187],[85,255,255],[85,85,255],[255,85,255],[85,85,85],[187,187,187]] 127 inSize = (len(inCanvasMap[0]), len(inCanvasMap)) 128 outSize = [a*b for a,b in zip(inSize, outImgFontSize)] 129 outCurPos = [0, 0] 130 outImg = Image.new('RGBA', outSize, (*ColorsNormal[1], 255)) 131 outImgDraw = ImageDraw.Draw(outImg) 132 for inCurRow in range(len(inCanvasMap)): 133 for inCurCol in range(len(inCanvasMap[inCurRow])): 134 inCurCell = inCanvasMap[inCurRow][inCurCol] 135 outColours = [0, 0] 136 if inCurCell[2] & 0x01: 137 if inCurCell[3] != ' ': 138 if inCurCell[3] == '█': 139 outColours[1] = ColorsNormal[inCurCell[0]] 140 else: 141 outColours[0] = ColorsBold[inCurCell[0]] 142 outColours[1] = ColorsNormal[inCurCell[1]] 143 else: 144 outColours[1] = ColorsNormal[inCurCell[1]] 145 else: 146 if inCurCell[3] != ' ': 147 if inCurCell[3] == '█': 148 outColours[1] = ColorsNormal[inCurCell[0]] 149 else: 150 outColours[0] = ColorsNormal[inCurCell[0]] 151 outColours[1] = ColorsNormal[inCurCell[1]] 152 else: 153 outColours[1] = ColorsNormal[inCurCell[1]] 154 outImgDraw.rectangle((*outCurPos,outCurPos[0] + outImgFontSize[0], outCurPos[1] + outImgFontSize[1]), fill=(*outColours[1], 255)) 155 if not inCurCell[3] in ' █' and outColours[0] != outColours[1]: 156 outImgDraw.text(outCurPos,inCurCell[3], (*outColours[0], 255), outImgFont) 157 if inCurCell[2] & 0x04: 158 outColours[0] = ColorsNormal[inCurCell[0]] 159 outImgDraw.line(xy=(outCurPos[0], outCurPos[1] + (outImgFontSize[1] - 2), outCurPos[0] + outImgFontSize[0], outCurPos[1] + (outImgFontSize[1] - 2)), fill=(*outColours[0], 255)) 160 outCurPos[0] += outImgFontSize[0] 161 outCurPos[0] = 0 162 outCurPos[1] += outImgFontSize[1] 163 out_file = os.path.join('data','temp.png') 164 if os.path.isfile(out_file): 165 os.remove(out_file) 166 outImg.save(out_file)