Advertisement
here2share

# Tk_img_putalpha.py

Mar 17th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. # Tk_img_putalpha.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk, ImageDraw, ImageChops
  5.  
  6. root = Tk()
  7. root.title("Tk_putalpha")
  8. root.geometry("500x500")
  9. wi = 500
  10. he = 500
  11. w = Canvas(root, width=wi, height=he)
  12. w.pack()
  13.  
  14. dia = 128
  15. circle = Image.new("L", (dia * 4, dia * 4), 0)
  16. dctx = ImageDraw.Draw(circle)
  17. dctx.ellipse([dia, dia, dia * 3, dia * 3], fill=255)
  18. del dctx
  19.  
  20. offset = dia // 2
  21. r = ImageChops.offset(circle, offset, offset)
  22. g = ImageChops.offset(circle, -offset, offset)
  23. b = ImageChops.offset(circle, 0, -offset+12)
  24.  
  25. dimg = Image.merge("RGB", (r, g, b))
  26. mask = Image.eval(dimg.convert("L"), lambda p: 255 if p > 0 else 0)
  27. dimg.putalpha(mask)
  28. #
  29.  
  30. imgTk = ImageTk.PhotoImage(dimg)
  31. w.create_image(0, 0, anchor=NW, image=imgTk)
  32. root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement