here2share

# Tk_fade_out_image.py -- ZZZ

Aug 21st, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. # Tk_fade_out_image.py -- ZZZ unable figure this one out at the moment
  2.  
  3. from Tkinter import *
  4. from PIL import Image,ImageTk,ImageDraw
  5. import urllib2
  6. import sys
  7. import base64
  8.  
  9. t = Tk()
  10. t.title("Fade Out Image")
  11.  
  12. frame = Frame(t)
  13. frame.pack()
  14.  
  15. canvas = Canvas(frame, bg="gray", width=400, height=400)
  16. canvas.pack()
  17.  
  18. def fade():
  19.     global img
  20.     canvas.delete('all')
  21.     create1()
  22.     if img.image_alpha < 255:
  23.         #img.set_alpha(img.image_alpha)
  24.         canvas.create_image(150, 150, image=img)
  25.         print (img.image_alpha)
  26.         img.image_alpha += int(255/img.fade_frame_number)
  27.         t.after(img.milsec/img.fade_frame_number, fade)
  28.     else: btn.configure(state=NORMAL); img=''
  29.     create2()
  30. #
  31. img=''
  32. def fade_out():
  33.     global img
  34.     btn.configure(state=DISABLED)
  35.     img=image1
  36.     img.image_alpha = 0
  37.     img.fade_frame_number = 30
  38.     img.milsec = 3000 # 3 second fade
  39.     fade()
  40. #
  41. solidball = "solidball.png"
  42. example = "example.png"
  43.  
  44. try:
  45.     image1 = ImageTk.PhotoImage(file=solidball)
  46.     image2 = ImageTk.PhotoImage(file=example)
  47.     print 'Images copied from URL to drive'
  48. except:
  49.     def oURL(dd):
  50.         URL = 'http://tinyurl.com/h2sball00'+str(dd)
  51.  
  52.         u = urllib2.urlopen(URL)
  53.         raw_data = u.read()
  54.         u.close()
  55.         b64_data = base64.encodestring(raw_data)
  56.         return PhotoImage(data=b64_data)
  57.     image1 = oURL(1)
  58.     image2 = oURL(2)
  59.     image1.save()
  60.     image2.save()
  61. #
  62.  
  63. def create1():
  64.     canvas.create_rectangle((100, 100, 300, 300), fill="darkgray")
  65. def create2():
  66.     canvas.create_image(250, 250, image=image2)
  67. create1()
  68. canvas.create_image(150, 150, image=image1)
  69. create2()
  70.  
  71. btn = Button(t, text=" Click To Fade Away An Image... ", command=fade_out)
  72. btn.pack()
  73.  
  74. t.mainloop()
  75.  
  76. photo1 = Image.open(solidball)
  77. photo2 = Image.open(example)
  78.  
  79. base, overlay = photo1, photo2
  80.  
  81. bands = list(overlay.split())
  82. if len(bands) == 4:
  83.     # Assuming alpha is the last band
  84.     bands[3] = bands[3].point(lambda x: x*0.5)
  85. overlay = Image.merge(overlay.mode, bands)
  86.  
  87. base.paste(overlay, (0, 0), overlay)
  88.  
  89. base.show()
Add Comment
Please, Sign In to add comment