Advertisement
SnowyCoder

noVNC screen decoder

Apr 23rd, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.29 KB | None | 0 0
  1. import base64
  2. import time
  3. from io import BytesIO
  4.  
  5. import pyocr.builders
  6. import selenium.webdriver.support.ui as ui
  7. from PIL import Image
  8. from selenium import webdriver
  9.  
  10. SITE = '' # ovh noVnc site
  11.  
  12.  
  13. tools = pyocr.get_available_tools()
  14. tool = tools[0]
  15.  
  16. ff = webdriver.Firefox()
  17. ff.get(SITE)
  18.  
  19. wait = ui.WebDriverWait(ff, 100)
  20.  
  21. canvas = wait.until(lambda ff: ff.find_element_by_css_selector('canvas'))
  22.  
  23. time.sleep(3)
  24.  
  25.  
  26. def decode_screen():
  27.     imgb64 = ff.execute_script("return arguments[0].toDataURL('image/png').substring(21);", canvas)
  28.     image = Image.open(BytesIO(base64.b64decode(imgb64)))
  29.     return image
  30.  
  31.  
  32. offsetx = 10
  33. offsety = 10
  34. sizex = 8
  35. sizey = 16
  36.  
  37. transbox = {
  38.     0b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: ' ',
  39.     0b00000000000000000000000000000000000110000010010001000010010001100100101001010010011000100100001000100100000110000000000000000000: '0',
  40.     0b00000000000000000000000000000000000010000001100000101000000010000000100000001000000010000000100000001000001111100000000000000000: '1',
  41.     0b00000000000000000000000000000000001111000100001001000010000000100000110000010000001000000100000001000000011111100000000000000000: '2',
  42.     0b00000000000000000000000000000000001111000100001001000010000000100001110000000010000000100100001001000010001111000000000000000000: '3',
  43.     0b00000000000000000000000000000000000001000000110000010100001001000100010001000100011111100000010000000100000001000000000000000000: '4',
  44.     0b00000000000000000000000000000000011111100100000001000000010000000111110000000010000000100000001001000010001111000000000000000000: '5',
  45.     0b00000000000000000000000000000000000111000010000001000000010000000111110001000010010000100100001001000010001111000000000000000000: '6',
  46.     0b00000000000000000000000000000000011111100000001000000010000001000000010000000100000010000000100000001000000010000000000000000000: '7',
  47.     0b00000000000000000000000000000000001111000100001001000010010000100011110001000010010000100100001001000010001111000000000000000000: '8',
  48.     0b00000000000000000000000000000000001111000100001001000010010000100011111000000010000000100000001000000100001110000000000000000000: '9',
  49.     0b00000000000000000000000000000000000000000000000000111100010000100000001000111110010000100100001001000110001110100000000000000000: 'a',
  50.     0b00000000000000000000000001000000010000000100000001011100011000100100001001000010010000100100001001100010010111000000000000000000: 'b',
  51.     0b00000000000000000000000000000000000000000000000000111100010000100100000001000000010000000100000001000010001111000000000000000000: 'c',
  52.     0b00000000000000000000000000000010000000100000001000111010010001100100001001000010010000100100001001000110001110100000000000000000: 'd',
  53.     0b00000000000000000000000000000000000000000000000000111100010000100100001001111110010000000100000001000010001111000000000000000000: 'e',
  54.     0b00000000000000000000000000001100000100000001000000010000011111000001000000010000000100000001000000010000000100000000000000000000: 'f',
  55.     0b00000000000000000000000000000000000000000000001000111010010001000100010001000100001110000010000000111100010000100100001000111100: 'g',
  56.     0b00000000000000000000000001000000010000000100000001011100011000100100001001000010010000100100001001000010010000100000000000000000: 'h',
  57.     0b00000000000000000000000000001000000010000000000000011000000010000000100000001000000010000000100000001000001111100000000000000000: 'i',
  58.     0b00000000000000000000000000000100000001000000000000001100000001000000010000000100000001000000010000000100000001000100100000110000: 'j',
  59.     0b00000000000000000000000001000000010000000100000001000100010010000101000001100000010100000100100001000100010000100000000000000000: 'k',
  60.     0b00000000000000000000000000011000000010000000100000001000000010000000100000001000000010000000100000001000001111100000000000000000: 'l',
  61.     0b00000000000000000000000000000000000000000000000001110110010010010100100101001001010010010100100101001001010010010000000000000000: 'm',
  62.     0b00000000000000000000000000000000000000000000000001011100011000100100001001000010010000100100001001000010010000100000000000000000: 'n',
  63.     0b00000000000000000000000000000000000000000000000000111100010000100100001001000010010000100100001001000010001111000000000000000000: 'o',
  64.     0b00000000000000000000000000000000000000000000000001011100011000100100001001000010010000100100001001100010010111000100000001000000: 'p',
  65.     0b00000000000000000000000000000000000000000000000000111010010001100100001001000010010000100100001001000110001110100000001000000010: 'q',
  66.     0b00000000000000000000000000000000000000000000000001011100011000100100001001000000010000000100000001000000010000000000000000000000: 'r',
  67.     0b00000000000000000000000000000000000000000000000000111100010000100100000000110000000011000000001001000010001111000000000000000000: 's',
  68.     0b00000000000000000000000000000000000100000001000000010000011111000001000000010000000100000001000000010000000011000000000000000000: 't',
  69.     0b00000000000000000000000000000000000000000000000001000010010000100100001001000010010000100100001001000110001110100000000000000000: 'u',
  70.     0b00000000000000000000000000000000000000000000000001000010010000100100001000100100001001000010010000011000000110000000000000000000: 'v',
  71.     0b00000000000000000000000000000000000000000000000001000001010010010100100101001001010010010100100101001001001101100000000000000000: 'w',
  72.     0b00000000000000000000000000000000000000000000000001000010010000100010010000011000000110000010010001000010010000100000000000000000: 'x',
  73.     0b00000000000000000000000000000000000000000000000001000010010000100100001001000010010000100010011000011010000000100000001000111100: 'y',
  74.     0b00000000000000000000000000000000000000000000000001111110000000100000010000001000000100000010000001000000011111100000000000000000: 'z',
  75.     0b00000000000000000000000000000000000110000010010000100100010000100100001001111110010000100100001001000010010000100000000000000000: 'A',
  76.     0b00000000000000000000000000000000011111000100001001000010010000100111110001000010010000100100001001000010011111000000000000000000: 'B',
  77.     0b00000000000000000000000000000000001111000100001001000010010000000100000001000000010000000100001001000010001111000000000000000000: 'C',
  78.     0b00000000000000000000000000000000011110000100010001000010010000100100001001000010010000100100001001000100011110000000000000000000: 'D',
  79.     0b00000000000000000000000000000000011111100100000001000000010000000111110001000000010000000100000001000000011111100000000000000000: 'E',
  80.     0b00000000000000000000000000000000011111100100000001000000010000000111110001000000010000000100000001000000010000000000000000000000: 'F',
  81.     0b00000000000000000000000000000000001111000100001001000010010000000100000001001110010000100100001001000110001110100000000000000000: 'G',
  82.     0b00000000000000000000000000000000010000100100001001000010010000100111111001000010010000100100001001000010010000100000000000000000: 'H',
  83.     0b00000000000000000000000000000000001111100000100000001000000010000000100000001000000010000000100000001000001111100000000000000000: 'I',
  84.     0b00000000000000000000000000000000000111110000010000000100000001000000010000000100000001000100010001000100001110000000000000000000: 'J',
  85.     0b00000000000000000000000000000000010000100100010001001000010100000110000001100000010100000100100001000100010000100000000000000000: 'K',
  86.     0b00000000000000000000000000000000010000000100000001000000010000000100000001000000010000000100000001000000011111100000000000000000: 'L',
  87.     0b00000000000000000000000000000000010000100100001001100110011001100101101001011010010000100100001001000010010000100000000000000000: 'M',
  88.     0b00000000000000000000000000000000010000100110001001100010010100100101001001001010010010100100011001000110010000100000000000000000: 'N',
  89.     0b00000000000000000000000000000000001111000100001001000010010000100100001001000010010000100100001001000010001111000000000000000000: 'O',
  90.     0b00000000000000000000000000000000011111000100001001000010010000100111110001000000010000000100000001000000010000000000000000000000: 'P',
  91.     0b00000000000000000000000000000000001111000100001001000010010000100100001001000010010000100101101001100110001111000000001100000000: 'Q',
  92.     0b00000000000000000000000000000000011111000100001001000010010000100111110001001000010001000100010001000010010000100000000000000000: 'R',
  93.     0b00000000000000000000000000000000001111000100001001000010010000000011000000001100000000100100001001000010001111000000000000000000: 'S',
  94.     0b00000000000000000000000000000000011111110000100000001000000010000000100000001000000010000000100000001000000010000000000000000000: 'T',
  95.     0b00000000000000000000000000000000010000100100001001000010010000100100001001000010010000100100001001000010001111000000000000000000: 'U',
  96.     0b00000000000000000000000000000000010000010100000101000001001000100010001000100010000101000001010000001000000010000000000000000000: 'V',
  97.     0b00000000000000000000000000000000010000100100001000100100001001000001100000011000001001000010010001000010010000100000000000000000: 'X',
  98.     0b00000000000000000000000000000000010000010100000100100010001000100001010000001000000010000000100000001000000010000000000000000000: 'Y',
  99.     0b00000000000000000000000000000000011111100000001000000010000001000000100000010000001000000100000001000000011111100000000000000000: 'Z',
  100.     0b00000000000000000000000000000000000010000000100000001000000010000000100000001000000010000000000000001000000010000000000000000000: '!',
  101.     0b00000000000000000000000000000000010000000100000000100000000100000001000000001000000010000000010000000010000000100000000000000000: '\\',
  102.     0b00000000000000000010001000100010001000100010001000000000000000000000000000000000000000000000000000000000000000000000000000000000: '"',
  103.     0b00000000000000000000000000000000000100100001001000010010011111100010010000100100011111100100100001001000010010000000000000000000: '#',
  104.     0b00000000000000000000000000000000000010000011111001001001010010000011100000001110000010010100100100111110000010000000000000000000: '$',
  105.     0b00000000000000000000000000000000001100010100101001001010001101000000100000001000000101100010100100101001010001100000000000000000: '%',
  106.     0b00000000000000000000000000000000000111000010001000100010000101000001100000101001010001010100001001000110001110010000000000000000: '&',
  107.     0b00000000000000000000100000001000000010000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000: '\'',
  108.     0b00000000000000000000000000000100000010000000100000010000000100000001000000010000000100000001000000001000000010000000010000000000: '(',
  109.     0b00000000000000000000000000100000000100000001000000001000000010000000100000001000000010000000100000010000000100000010000000000000: ')',
  110.     0b00000000000000000000000000000000000000000000000000001000010010010010101000011100001010100100100100001000000000000000000000000000: '*',
  111.     0b00000000000000000000000000000000000000000000000000001000000010000000100001111111000010000000100000001000000000000000000000000000: '+',
  112.     0b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000100000010000: ',',
  113.     0b00000000000000000000000000000000000000000000000000000000000000000000000000111100000000000000000000000000000000000000000000000000: '-',
  114.     0b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000110000000000000000000: '.',
  115.     0b00000000000000000000000000000000000000100000001000000100000010000000100000010000000100000010000001000000010000000000000000000000: '/',
  116.     0b00000000000000000000000000000000000000000000000000011000000110000000000000000000000000000001100000011000000000000000000000000000: ':',
  117.     0b00000000000000000000000000000000000000000000000000011000000110000000000000000000000000000001100000001000000010000001000000000000: ';',
  118.     0b00000000000000000000000000000000000000000000001000000100000010000001000000100000000100000000100000000100000000100000000000000000: '<',
  119.     0b00000000000000000000000000000000000000000000000000000000011111100000000000000000000000000111111000000000000000000000000000000000: '=',
  120.     0b00000000000000000000000000000000000000000100000000100000000100000000100000000100000010000001000000100000010000000000000000000000: '>',
  121. }
  122.  
  123.  
  124. def charbox_at(x, y):
  125.     startx = offsetx + sizex * x
  126.     starty = offsety + sizey * y
  127.     box = (startx, starty, startx + sizex, starty + sizey)
  128.     return box
  129.  
  130.  
  131. def char_image(image, x, y):
  132.     return image.crop(charbox_at(x, y))
  133.  
  134.  
  135. def char_to_hashint(image, charbox):
  136.     data = [1 if x != 0 else 0 for x in image.crop(charbox).tobytes()[::4]]
  137.     # return ''.join([str(x) for x in data])
  138.     res = 0
  139.     for bit in data:
  140.         res = (res << 1) | bit
  141.     return res
  142.  
  143.  
  144. def decode_char_at(image, x, y):
  145.     chbox = charbox_at(x, y)
  146.     chsign = char_to_hashint(image, chbox)
  147.     if chsign in transbox:
  148.         return transbox[chsign]
  149.     else:
  150.         return '?'
  151.  
  152.  
  153. def decode_image(image):
  154.     resw = image.width // sizex
  155.     resh = image.height // sizey
  156.     res = [0] * (resw * resh)
  157.     for y in range(0, resh):
  158.         for x in range(0, resw):
  159.             res[resw * y + x] = decode_char_at(image, x, y)
  160.  
  161.     return res
  162.  
  163.  
  164. if __name__ == '__main__':
  165.     image = decode_screen()
  166.     txtimg = decode_image(image)
  167.  
  168.     image.show()
  169.     print(txtimg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement