Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_b64_zlib_img.py
- from Tkinter import *
- import tkFileDialog
- from cStringIO import StringIO
- from PIL import ImageTk, Image
- import base64, zlib
- root = Tk()
- canvas = Canvas(root,width=400,height=640)
- canvas.pack()
- oFiletype =("png files","*.png"),("all files","*.*")
- def b64_zlib():
- """Convert a file (specified by a path) into a data URI."""
- path = tkFileDialog.askopenfilename(title = "Select file",filetypes=oFiletype)
- try:
- with open(path, 'rb') as fp:
- data = fp.read()
- compressed = zlib.compress(data)
- b64 = base64.encodestring(compressed)
- return b64
- except:
- print ">>> acceptable image not selected <<<"
- b64 = b64_zlib()
- if b64:
- print "b64_zlib='''\\\n"+b64+"'''"
- b64 = base64.decodestring(b64)
- zzz = StringIO(zlib.decompress(b64))
- img_data = Image.open(zzz)
- img = ImageTk.PhotoImage(img_data)
- #
- w,h = img.width(),img.height()
- canvas.create_image((w/3+2,h/3+2),image=img)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement