Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. from jaraco.video import capture
  2. from PIL import Image, ImageTk
  3. import tkinter as tk
  4.  
  5. def animate():
  6. image = cam.get_image()
  7. image = image.crop((crop['left'], crop['top'], width - crop['right'], height - crop['bottom']))
  8. image = image.resize((int(image.size[0] / pixelsize), int(image.size[1] / pixelsize)), Image.NEAREST)
  9. image = image.resize((int(image.size[0] * pixelsize), int(image.size[1] * pixelsize)), Image.NEAREST)
  10. padimage = Image.new('RGB', (width, height), 0x000000)
  11. padimage.paste(image, (int((width - image.size[0]) / 2), int((height - image.size[1]) / 2)))
  12. photo = ImageTk.PhotoImage(padimage)
  13. label.config(image=photo)
  14. label.image = photo
  15. label.after(1, animate)
  16.  
  17. def mouse1drag(event):
  18. global m2down, pressx, pixelsize, presspixelsize
  19. if m2down: return
  20. pixelsize = calcint(presspixelsize, (event.x - pressx), 0.05, 1, 100)
  21.  
  22. def mouse2drag(event):
  23. global m1down, presscrop, pressx, pressy, crop, cropindex, width, height
  24. if m1down: return
  25. fun = lambda dx, ymax: calcint(presscrop, dx, 1, 0, ymax)
  26. if cropindex == 'top':
  27. val = fun((event.y - pressy), height / 2 - 50)
  28. if cropindex == 'bottom':
  29. val = fun(-(event.y - pressy), height / 2 - 50)
  30. if cropindex == 'left':
  31. val = fun((event.x - pressx), width / 2 - 50)
  32. if cropindex == 'right':
  33. val = fun(-(event.x - pressx), width / 2 - 50)
  34. crop[cropindex] = val
  35.  
  36. def mousepress(event):
  37. global m1down, m2down, pressx, pressy, pixelsize, presspixelsize, crop, presscrop, cropindex
  38. if event.num == 1:
  39. if m2down: return
  40. m1down = True
  41. elif event.num == 3:
  42. if m1down: return
  43. m2down = True
  44. pressx = event.x
  45. pressy = event.y
  46. presspixelsize = pixelsize
  47. cropindex = getcardinal(event.x, event.y)
  48. presscrop = crop[cropindex]
  49.  
  50. def mouserelease(event):
  51. global m1down, m2down
  52. if event.num == 1:
  53. m1down = False
  54. elif event.num == 3:
  55. m2down = False
  56.  
  57. def calcint(y0, dx, slope, ymin, ymax):
  58. return int(max(min(y0 + dx * slope, ymax), ymin))
  59.  
  60. def getcardinal(x, y):
  61. global width, height
  62. aboveneg = y < x * height / width
  63. abovepos = y < -x * height / width + height
  64. if abovepos and aboveneg: return 'top'
  65. elif not abovepos and not aboveneg: return 'bottom'
  66. elif abovepos and not aboveneg: return 'left'
  67. elif not abovepos and aboveneg: return 'right'
  68.  
  69. m1down = False
  70. m2down = False
  71. pixelsize = 40
  72. crop = { 'top': 0, 'bottom': 0, 'left': 0, 'right': 0 }
  73. displaymode = 'pad'
  74.  
  75. cam = capture.Device(0, 0)
  76. width, height = cam.get_image().size
  77.  
  78. root = tk.Tk()
  79. root.wm_title('pixcam')
  80. root.resizable(width=False, height=False)
  81. label = tk.Label(root, borderwidth=0)
  82. label.bind("<ButtonPress-1>", mousepress)
  83. label.bind("<ButtonPress-3>", mousepress)
  84. label.bind("<ButtonRelease-1>", mouserelease)
  85. label.bind("<ButtonRelease-3>", mouserelease)
  86. label.bind("<B1-Motion>", mouse1drag)
  87. label.bind("<B3-Motion>", mouse2drag)
  88. label.pack()
  89. animate()
  90. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement