Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tempfile
- # byte literal code for a transparent icon, I think
- ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
- b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
- b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
- b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
- # makes a temp file for the transparent icon and saves it
- _, ICON_PATH = tempfile.mkstemp()
- with open(ICON_PATH, 'wb') as icon_file:
- icon_file.write(ICON)
- import tempfile, base64, io
- # byte literal code for a transparent icon, I think
- ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
- b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
- b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
- b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
- # makes a temp file for the transparent icon and saves it
- _, ICON_PATH = tempfile.mkstemp()
- with open(ICON_PATH, 'wb') as icon_file:
- icon_file.write(ICON)
- a = open(ICON_PATH, 'rb').read()
- b = base64.b64encode(a)
- print b # output does not match what ICON equals above
- # doesn't work -- gives error UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 1342: invalid start byte
- #b = bytes(a).decode('utf-8')
- c = bytearray(a)
- print c # prints garbled junk
- # gives error AttributeError: __exit__ on Image.open(ICON_PATH) line
- with io.BytesIO() as output:
- from PIL import Image
- with Image.open(ICON_PATH) as img:
- img.convert('RGB').save(output, 'BMP')
- data = output.getvalue()[14:]
- print data
- print(repr(a))
- import zlib
- print(repr(zlib.compress(a)))
- import zlib
- ICON = zlib.decompress(b'xx9cc``x04Bx01x01x06 xc9xc1x90xc1xcaxc0 '
- b'xc6xc0xc0xa0x01xc4@!x06x05x06x888x088xb02 x00#x14x8fx82'
- b'Q0nFxc1x08x05xff)x04x00Uxf1Ax17')
- import base64, zlib
- print(repr(base64.b64encode(zlib.compress(a))))
- import base64, zlib
- ICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
- 'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))
- import base64
- print(base64.b64encode(png_icon))
- PNG_ICON = base64.b64decode( ** insert literal here ** )
- print(repr(a.encode('zlib').encode('base64')))
- ICON = ('eJxjYGAEQgEBBiDJwZDBysAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4J'
- 'RMApGwQgF/ykEAFXxQRc=').decode('base64').decode('zlib')
- from itertools import izip
- import tempfile
- # byte literal code for a transparent icon, I think
- ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
- b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
- b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
- b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
- # make a temp file from ICON data for testing
- _, ICON_PATH = tempfile.mkstemp()
- with open(ICON_PATH, 'wb') as icon_file:
- icon_file.write(ICON)
- # Convert raw data in the file into a valid Python string literal.
- # helper function
- def grouper(n, seq):
- "s -> (s0,s1,...sn-1), (sn,sn+1,...s2n-1), (s2n,s2n+1,...s3n-1), ..."
- for i in xrange(0, len(seq), n):
- yield seq[i:i+n]
- # read data file in binary mode
- a = open(ICON_PATH, 'rb').read()
- # create Python code to define string literal
- code = 'n'.join(['ICON2 = ('] +
- [' '+repr(group) for group in grouper(16, a)] +
- [')'])
- print 'len(ICON): {}'.format(len(ICON))
- print 'len(a): {}'.format(len(a))
- print code
- exec(code)
- print
- print 'len(ICON2): {}'.format(len(ICON2))
- print 'ICON2 == ICON: {}'.format(ICON2 == ICON)
- import tempfile, base64, io
- # byte literal code for a transparent icon, I think
- ICON = (b'x00x00x01x00x01x00x10x10x00x00x01x00x08x00hx05x00x00'
- b'x16x00x00x00(x00x00x00x10x00x00x00 x00x00x00x01x00'
- b'x08x00x00x00x00x00@x05x00x00x00x00x00x00x00x00x00x00'
- b'x00x01x00x00x00x01') + b'x00'*1282 + b'xff'*64
- # makes a temp file for the transparent icon and saves it
- _, ICON_PATH = tempfile.mkstemp()
- with open(ICON_PATH, 'wb') as icon_file:
- icon_file.write(ICON)
- with open(ICON_PATH, 'rb') as imgFile:
- a = imgFile.read()
- b = base64.b64encode(a)
- print b # output does not match what ICON equals above
- with open('test.png','wb') as writeFile:
- writeFile.write(b.decode('base64'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement