-
Notifications
You must be signed in to change notification settings - Fork 74
/
focus-snap.py
55 lines (46 loc) · 1.4 KB
/
focus-snap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import piggyphoto, pygame
import os
import time
import Image, ImageFilter, ImageChops, ImageStat
from collections import deque
import focus
def quit_pressed():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
return False
def show(file):
picture = pygame.image.load(file)
picture = pygame.transform.scale(picture, (1056,704))
main_surface.blit(picture, (0, 0))
pygame.display.flip()
C = piggyphoto.camera()
C.leave_locked()
C.capture_preview('preview.jpg')
picture = pygame.image.load("preview.jpg")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
Q = deque()
k = 1
looking_for_peak = True
while not quit_pressed():
C.capture_preview('preview.jpg')
show("preview.jpg")
f = focus.estimate("preview.jpg")
Q.append(f)
if len(Q) > 20:
Q.popleft()
F = max(Q)
pygame.display.set_caption("Focus: %.4g / %.4g / %s" % (f, F, "Looking for focus peak" if looking_for_peak else "Focus peak found"))
if len(Q) == 20:
if looking_for_peak:
if f < F * 0.8:
looking_for_peak = False
else:
if f > F * 0.98:
C.capture_image("snap%d.jpg" % k)
show("snap%d.jpg" % k)
k = k + 1
Q = deque()
time.sleep(1)
looking_for_peak = True