Advertisement
here2share

# Tk_b64_zlib_img.py

Jun 5th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. # Tk_b64_zlib_img.py
  2.  
  3. from Tkinter import *
  4. import tkFileDialog
  5. from cStringIO import StringIO
  6. from PIL import ImageTk, Image
  7. import base64, zlib
  8.  
  9. root = Tk()
  10. canvas = Canvas(root,width=400,height=640)
  11. canvas.pack()
  12.  
  13. oFiletype =("png files","*.png"),("all files","*.*")
  14. def b64_zlib():
  15.     """Convert a file (specified by a path) into a data URI."""
  16.     path = tkFileDialog.askopenfilename(title = "Select file",filetypes=oFiletype)
  17.     try:
  18.         with open(path, 'rb') as fp:
  19.             data = fp.read()
  20.             compressed = zlib.compress(data)
  21.             b64 = base64.encodestring(compressed)
  22.         return b64
  23.     except:
  24.         print ">>> acceptable image not selected <<<"
  25.  
  26. b64 = b64_zlib()
  27. if b64:
  28.     print "b64_zlib='''\\\n"+b64+"'''"
  29.     b64 = base64.decodestring(b64)
  30.     zzz = StringIO(zlib.decompress(b64))
  31.     img_data = Image.open(zzz)
  32.     img = ImageTk.PhotoImage(img_data)
  33. #
  34. w,h = img.width(),img.height()
  35. canvas.create_image((w/3+2,h/3+2),image=img)
  36. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement