-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlay_functions.py
72 lines (51 loc) · 2.13 KB
/
overlay_functions.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Adapted from some original code by bennuttall and waveform80
# -------------------------------------------------------------
import PIL
from PIL import Image
from itertools import cycle
# EDIT THESE VALUES ------------------------
overlays_dir = "/home/pi/fotomaton/overlays"
overlays = ['burbujass','Fotopulpo','logo','sapoconcho','pulpos','curiosity','badgre']
# ------------------------------------------
overlay = overlays[0] # Starting value
def _get_overlay_image(overlay):
# Open the overlay as an Image object
return Image.open(overlays_dir + "/" + overlay + ".png")
def _pad(resolution, width=32, height=16):
# Pads the specified resolution
# up to the nearest multiple of *width* and *height*; this is
# needed because overlays require padding to the camera's
# block size (32x16)
return (
((resolution[0] + (width - 1)) // width) * width,
((resolution[1] + (height - 1)) // height) * height,
)
def remove_overlays(camera):
# Remove all overlays from the camera preview
for o in camera.overlays:
camera.remove_overlay(o)
def preview_overlay(camera=None, overlay=None):
# Get an Image object of the chosen overlay
overlay_img = _get_overlay_image(overlay)
# Pad it to the right resolution
pad = Image.new('RGBA', _pad(camera.resolution))
pad.paste(overlay_img, (0, 0))
# Add the overlay
camera.add_overlay(pad.tobytes(), layer=3)
def preview_overlay_o(camera=None, overlay=None):
# Get an Image object of the chosen overlay
overlay_img = _get_overlay_image(overlay)
# Pad it to the right resolution
pad = Image.new('RGBA', _pad(camera.resolution))
pad.paste(overlay_img, (0, 0))
# Add the overlay
camera.add_overlay(pad.tobytes(), layer=4)
def output_overlay(output=None, overlay=None):
# Take an overlay Image
overlay_img = _get_overlay_image(overlay)
# ...and a captured photo
output_img = Image.open(output).convert('RGBA')
# Combine the two and save the image as output
new_output = Image.alpha_composite(output_img, overlay_img)
new_output.save(output)
all_overlays = cycle(overlays)