Advertisement
here2share

# Tk_gif2b64string.py ^ 2017.07

Jul 21st, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. # Tk_gif2b64string.py
  2. # note: GIFs only
  3.  
  4. from Tkinter import *
  5. import tkFileDialog
  6. from cStringIO import StringIO
  7. from PIL import ImageTk, Image
  8. import base64
  9.  
  10. root = Tk()
  11. canvas = Canvas(root,width=400,height=640)
  12. canvas.pack()
  13.  
  14. b64A='R0lGODlhKQBaAPcAAAAAA'
  15. b64B='A'*1023+'''CH5BAEAAP8ALAAAAAApAFoA
  16. AAj/AP8JHEiw4MAACBEaXMiwYcGEEB1KnAix4sSLCytGxMjxn0aLHS9+3BjS4UiQJRmeJJny4UqW
  17. LQW+hBlzZsKYBG3SXAigp0+XOgMU9PmTKNGcQYUONMrUKNKgBJtKBXAwqUKBU5tWtbo0q1OZVpX+
  18. 8/rVY9iuZHtuTYo27VqoWNOqBcs1rly6Z8fKpWo2rNK9fP1eBYy3LuG+eQ8L/ru3MFu9dxEbbiz5
  19. sWLBdt1WhntZo4DPn0dmJusYtOnQIKVC/ln4tGuUTCVWdH0aZUiItF/fTIk7t+mdIhP6/r27pPDh
  20. xMXeRog8ecvjoI3WvmqcuQCvqJV3PI5dQPHlAZqD5P6+3bp48hyhN0ePUT1y9sHDi/dOHfx8+trT
  21. m19fv7z88/3p9x9/+Q1VlnvDFReban3dt1tnug2Y3Whe0ZVcbxOuRlqD43nWIWORQXfShxRmZWF2
  22. HuJX4lR4oTgbfiBq1tuIMK7I4IwjoWijVoi5iONglL34kYs7xiaZj/KlFuSL+H344JIc0nYTkCEK
  23. heGFZhX51VVMgkSljErNVJWWR2XpGEpfbljfSUiRyVp/H7nk5lwB9pVRjGoWaBKeFZqp34oa8uVn
  24. e2IZ2VCde8q2GFeLNuroo5BGKumklFZqaaWbWfpPQAA7'''
  25.  
  26. rc={}
  27. rc[1]=b64A+'Fj/DP///w'+b64B
  28. rc[2]=b64A+'P8AAP///w'+b64B
  29. rc[3]=b64A+'CG1/////w'+b64B
  30.  
  31. obj = {1: (50,50), 2: (80,90), 3: (200,560), 4: (200,200)}
  32.  
  33. for z in rc:
  34.     b64 = StringIO(base64.decodestring(rc[z]))
  35.     b64 = Image.open(b64)
  36.     rc[z] = ImageTk.PhotoImage(b64)
  37.     canvas.create_image(obj[z],image=rc[z])
  38.  
  39. class Cv():
  40.     x,y=0,0
  41.     xx,yy=0,0
  42.     objv=False
  43. cv=Cv()
  44.  
  45. def click(event):
  46.     x, y = (event.x, event.y)
  47.     cv.objv = canvas.find_closest(x,y)[0]
  48.     cv.xx, cv.yy = obj[cv.objv]
  49.     if x < cv.xx+32 and x > cv.xx-32 and y < cv.yy+32 and y > cv.yy-32:
  50.         cv.xx, cv.yy = cv.xx-x, cv.yy-y
  51.         canvas.lift(cv.objv) # vs canvas.lower()
  52.     else: cv.objv = False
  53.    
  54.  
  55. def drag(event):
  56.     if cv.objv:
  57.         x, y = (event.x, event.y)
  58.         z = cv.xx+x, cv.yy+y
  59.         canvas.coords(cv.objv, z)
  60.         obj[cv.objv] = z
  61.  
  62. canvas.bind('<Button-1>',click)
  63. canvas.bind('<B1-Motion>',drag)
  64.  
  65.  
  66. oFiletype =("gif files","*.gif"),("all files","*.*")
  67. def img_to_b64():
  68.     """Convert a file (specified by a path) into a data URI."""
  69.     path = tkFileDialog.askopenfilename(title = "Select file",filetypes=oFiletype)
  70.     try:
  71.         with open(path, 'rb') as fp:
  72.             data = fp.read()
  73.             data64 = base64.encodestring(data)
  74.         return data64
  75.     except:
  76.         print ">>> GIF not selected <<<"
  77.  
  78. b64 = img_to_b64()[:-1]
  79. if b64:
  80.     print "py_gif_b64='''\\\n"+b64+"'''"
  81.     f = StringIO(base64.decodestring(b64))
  82.     b64 = Image.open(f)
  83.     rc[4] = ImageTk.PhotoImage(b64)
  84.  
  85. canvas.create_image(obj[4],image=rc[4])
  86. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement