Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from jaraco.video import capture
- from PIL import Image, ImageTk
- import tkinter as tk
- def animate():
- image = cam.get_image()
- image = image.crop((crop['left'], crop['top'], width - crop['right'], height - crop['bottom']))
- image = image.resize((int(image.size[0] / pixelsize), int(image.size[1] / pixelsize)), Image.NEAREST)
- image = image.resize((int(image.size[0] * pixelsize), int(image.size[1] * pixelsize)), Image.NEAREST)
- padimage = Image.new('RGB', (width, height), 0x000000)
- padimage.paste(image, (int((width - image.size[0]) / 2), int((height - image.size[1]) / 2)))
- photo = ImageTk.PhotoImage(padimage)
- label.config(image=photo)
- label.image = photo
- label.after(1, animate)
- def mouse1drag(event):
- global m2down, pressx, pixelsize, presspixelsize
- if m2down: return
- pixelsize = calcint(presspixelsize, (event.x - pressx), 0.05, 1, 100)
- def mouse2drag(event):
- global m1down, presscrop, pressx, pressy, crop, cropindex, width, height
- if m1down: return
- fun = lambda dx, ymax: calcint(presscrop, dx, 1, 0, ymax)
- if cropindex == 'top':
- val = fun((event.y - pressy), height / 2 - 50)
- if cropindex == 'bottom':
- val = fun(-(event.y - pressy), height / 2 - 50)
- if cropindex == 'left':
- val = fun((event.x - pressx), width / 2 - 50)
- if cropindex == 'right':
- val = fun(-(event.x - pressx), width / 2 - 50)
- crop[cropindex] = val
- def mousepress(event):
- global m1down, m2down, pressx, pressy, pixelsize, presspixelsize, crop, presscrop, cropindex
- if event.num == 1:
- if m2down: return
- m1down = True
- elif event.num == 3:
- if m1down: return
- m2down = True
- pressx = event.x
- pressy = event.y
- presspixelsize = pixelsize
- cropindex = getcardinal(event.x, event.y)
- presscrop = crop[cropindex]
- def mouserelease(event):
- global m1down, m2down
- if event.num == 1:
- m1down = False
- elif event.num == 3:
- m2down = False
- def calcint(y0, dx, slope, ymin, ymax):
- return int(max(min(y0 + dx * slope, ymax), ymin))
- def getcardinal(x, y):
- global width, height
- aboveneg = y < x * height / width
- abovepos = y < -x * height / width + height
- if abovepos and aboveneg: return 'top'
- elif not abovepos and not aboveneg: return 'bottom'
- elif abovepos and not aboveneg: return 'left'
- elif not abovepos and aboveneg: return 'right'
- m1down = False
- m2down = False
- pixelsize = 40
- crop = { 'top': 0, 'bottom': 0, 'left': 0, 'right': 0 }
- displaymode = 'pad'
- cam = capture.Device(0, 0)
- width, height = cam.get_image().size
- root = tk.Tk()
- root.wm_title('pixcam')
- root.resizable(width=False, height=False)
- label = tk.Label(root, borderwidth=0)
- label.bind("<ButtonPress-1>", mousepress)
- label.bind("<ButtonPress-3>", mousepress)
- label.bind("<ButtonRelease-1>", mouserelease)
- label.bind("<ButtonRelease-3>", mouserelease)
- label.bind("<B1-Motion>", mouse1drag)
- label.bind("<B3-Motion>", mouse2drag)
- label.pack()
- animate()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement