Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_gif2b64string.py
- # note: GIFs only
- from Tkinter import *
- import tkFileDialog
- from cStringIO import StringIO
- from PIL import ImageTk, Image
- import base64
- root = Tk()
- canvas = Canvas(root,width=400,height=640)
- canvas.pack()
- b64A='R0lGODlhKQBaAPcAAAAAA'
- b64B='A'*1023+'''CH5BAEAAP8ALAAAAAApAFoA
- AAj/AP8JHEiw4MAACBEaXMiwYcGEEB1KnAix4sSLCytGxMjxn0aLHS9+3BjS4UiQJRmeJJny4UqW
- LQW+hBlzZsKYBG3SXAigp0+XOgMU9PmTKNGcQYUONMrUKNKgBJtKBXAwqUKBU5tWtbo0q1OZVpX+
- 8/rVY9iuZHtuTYo27VqoWNOqBcs1rly6Z8fKpWo2rNK9fP1eBYy3LuG+eQ8L/ru3MFu9dxEbbiz5
- sWLBdt1WhntZo4DPn0dmJusYtOnQIKVC/ln4tGuUTCVWdH0aZUiItF/fTIk7t+mdIhP6/r27pPDh
- xMXeRog8ecvjoI3WvmqcuQCvqJV3PI5dQPHlAZqD5P6+3bp48hyhN0ePUT1y9sHDi/dOHfx8+trT
- m19fv7z88/3p9x9/+Q1VlnvDFReban3dt1tnug2Y3Whe0ZVcbxOuRlqD43nWIWORQXfShxRmZWF2
- HuJX4lR4oTgbfiBq1tuIMK7I4IwjoWijVoi5iONglL34kYs7xiaZj/KlFuSL+H344JIc0nYTkCEK
- heGFZhX51VVMgkSljErNVJWWR2XpGEpfbljfSUiRyVp/H7nk5lwB9pVRjGoWaBKeFZqp34oa8uVn
- e2IZ2VCde8q2GFeLNuroo5BGKumklFZqaaWbWfpPQAA7'''
- rc={}
- rc[1]=b64A+'Fj/DP///w'+b64B
- rc[2]=b64A+'P8AAP///w'+b64B
- rc[3]=b64A+'CG1/////w'+b64B
- obj = {1: (50,50), 2: (80,90), 3: (200,560), 4: (200,200)}
- for z in rc:
- b64 = StringIO(base64.decodestring(rc[z]))
- b64 = Image.open(b64)
- rc[z] = ImageTk.PhotoImage(b64)
- canvas.create_image(obj[z],image=rc[z])
- class Cv():
- x,y=0,0
- xx,yy=0,0
- objv=False
- cv=Cv()
- def click(event):
- x, y = (event.x, event.y)
- cv.objv = canvas.find_closest(x,y)[0]
- cv.xx, cv.yy = obj[cv.objv]
- if x < cv.xx+32 and x > cv.xx-32 and y < cv.yy+32 and y > cv.yy-32:
- cv.xx, cv.yy = cv.xx-x, cv.yy-y
- canvas.lift(cv.objv) # vs canvas.lower()
- else: cv.objv = False
- def drag(event):
- if cv.objv:
- x, y = (event.x, event.y)
- z = cv.xx+x, cv.yy+y
- canvas.coords(cv.objv, z)
- obj[cv.objv] = z
- canvas.bind('<Button-1>',click)
- canvas.bind('<B1-Motion>',drag)
- oFiletype =("gif files","*.gif"),("all files","*.*")
- def img_to_b64():
- """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()
- data64 = base64.encodestring(data)
- return data64
- except:
- print ">>> GIF not selected <<<"
- b64 = img_to_b64()[:-1]
- if b64:
- print "py_gif_b64='''\\\n"+b64+"'''"
- f = StringIO(base64.decodestring(b64))
- b64 = Image.open(f)
- rc[4] = ImageTk.PhotoImage(b64)
- canvas.create_image(obj[4],image=rc[4])
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement