Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. import tempfile
  2.  
  3. # byte literal code for a transparent icon, I think
  4. ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
  5. b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
  6. b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
  7. b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
  8.  
  9. # makes a temp file for the transparent icon and saves it
  10. _, ICON_PATH = tempfile.mkstemp()
  11. with open(ICON_PATH, 'wb') as icon_file:
  12. icon_file.write(ICON)
  13.  
  14. import tempfile, base64, io
  15.  
  16. # byte literal code for a transparent icon, I think
  17. ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
  18. b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
  19. b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
  20. b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
  21.  
  22. # makes a temp file for the transparent icon and saves it
  23. _, ICON_PATH = tempfile.mkstemp()
  24. with open(ICON_PATH, 'wb') as icon_file:
  25. icon_file.write(ICON)
  26.  
  27. a = open(ICON_PATH, 'rb').read()
  28.  
  29. b = base64.b64encode(a)
  30.  
  31. print b # output does not match what ICON equals above
  32.  
  33. # doesn't work -- gives error UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 1342: invalid start byte
  34. #b = bytes(a).decode('utf-8')
  35.  
  36. c = bytearray(a)
  37.  
  38. print c # prints garbled junk
  39.  
  40.  
  41. # gives error AttributeError: __exit__ on Image.open(ICON_PATH) line
  42. with io.BytesIO() as output:
  43. from PIL import Image
  44. with Image.open(ICON_PATH) as img:
  45. img.convert('RGB').save(output, 'BMP')
  46. data = output.getvalue()[14:]
  47. print data
  48.  
  49. print(repr(a))
  50.  
  51. import zlib
  52. print(repr(zlib.compress(a)))
  53.  
  54. import zlib
  55.  
  56. ICON = zlib.decompress(b'xx9cc``x04Bx01x01x06 xc9xc1x90xc1xcaxc0 '
  57. b'xc6xc0xc0xa0x01xc4@!x06x05x06x888x088xb02 x00#x14x8fx82'
  58. b'Q0nFxc1x08x05xff)x04x00Uxf1Ax17')
  59.  
  60. import base64, zlib
  61.  
  62. print(repr(base64.b64encode(zlib.compress(a))))
  63.  
  64. import base64, zlib
  65.  
  66. ICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
  67. 'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))
  68.  
  69. import base64
  70.  
  71. print(base64.b64encode(png_icon))
  72.  
  73. PNG_ICON = base64.b64decode( ** insert literal here ** )
  74.  
  75. print(repr(a.encode('zlib').encode('base64')))
  76.  
  77. ICON = ('eJxjYGAEQgEBBiDJwZDBysAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4J'
  78. 'RMApGwQgF/ykEAFXxQRc=').decode('base64').decode('zlib')
  79.  
  80. from itertools import izip
  81. import tempfile
  82.  
  83. # byte literal code for a transparent icon, I think
  84. ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
  85. b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
  86. b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
  87. b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
  88.  
  89. # make a temp file from ICON data for testing
  90. _, ICON_PATH = tempfile.mkstemp()
  91. with open(ICON_PATH, 'wb') as icon_file:
  92. icon_file.write(ICON)
  93.  
  94. # Convert raw data in the file into a valid Python string literal.
  95.  
  96. # helper function
  97. def grouper(n, seq):
  98. "s -> (s0,s1,...sn-1), (sn,sn+1,...s2n-1), (s2n,s2n+1,...s3n-1), ..."
  99. for i in xrange(0, len(seq), n):
  100. yield seq[i:i+n]
  101.  
  102. # read data file in binary mode
  103. a = open(ICON_PATH, 'rb').read()
  104.  
  105. # create Python code to define string literal
  106. code = 'n'.join(['ICON2 = ('] +
  107. [' '+repr(group) for group in grouper(16, a)] +
  108. [')'])
  109.  
  110. print 'len(ICON): {}'.format(len(ICON))
  111. print 'len(a): {}'.format(len(a))
  112. print code
  113. exec(code)
  114. print
  115. print 'len(ICON2): {}'.format(len(ICON2))
  116. print 'ICON2 == ICON: {}'.format(ICON2 == ICON)
  117.  
  118. import tempfile, base64, io
  119.  
  120. # byte literal code for a transparent icon, I think
  121. ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
  122. b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
  123. b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
  124. b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
  125.  
  126. # makes a temp file for the transparent icon and saves it
  127. _, ICON_PATH = tempfile.mkstemp()
  128. with open(ICON_PATH, 'wb') as icon_file:
  129. icon_file.write(ICON)
  130.  
  131. with open(ICON_PATH, 'rb') as imgFile:
  132. a = imgFile.read()
  133.  
  134. b = base64.b64encode(a)
  135.  
  136. print b # output does not match what ICON equals above
  137.  
  138. with open('test.png','wb') as writeFile:
  139. writeFile.write(b.decode('base64'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement