forked from adafruit/Adafruit_CircuitPython_PyCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
207 lines (164 loc) · 5.31 KB
/
code.py
File metadata and controls
207 lines (164 loc) · 5.31 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# SPDX-FileCopyrightText: 2024 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""Effects Demonstration
This will apply a nubmer of effects to a single image.
Press any of the directional buttons to immediately apply a new effect.
Otherwise, effects cycle every DISPLAY_INTERVAL milliseconds (default 2000 = 2 seconds)
"""
import displayio
from jpegio import JpegDecoder
from adafruit_display_text.label import Label
from adafruit_ticks import ticks_less, ticks_ms, ticks_add, ticks_diff
from font_free_mono_bold_24 import FONT
import bitmapfilter
from adafruit_pycamera import imageprocessing
from adafruit_pycamera import PyCameraBase
blend_50_50 = bitmapfilter.blend_precompute(imageprocessing.alphablend_maker(0.5))
screen = bitmapfilter.blend_precompute(imageprocessing.screen_func)
overlay = bitmapfilter.blend_precompute(imageprocessing.overlay_func)
hard_light = bitmapfilter.blend_precompute(imageprocessing.hard_light_func)
soft_light = bitmapfilter.blend_precompute(imageprocessing.soft_light_func)
color_dodge = bitmapfilter.blend_precompute(imageprocessing.color_dodge_func)
# linear_dodge = bitmapfilter.blend_precompute(imageprocessing.linear_dodge_func)
# divide = bitmapfilter.blend_precompute(imageprocessing.divide_func)
multiply = bitmapfilter.blend_precompute(imageprocessing.multiply_func)
# subtract = bitmapfilter.blend_precompute(imageprocessing.subtract_func)
# color_burn = bitmapfilter.blend_precompute(imageprocessing.color_burn_func)
# linear_burn = bitmapfilter.blend_precompute(imageprocessing.linear_burn_func)
# darken_only = bitmapfilter.blend_precompute(min)
# lighten_only = bitmapfilter.blend_precompute(max)
def blender(f):
def inner(b):
return bitmapfilter.blend(b, b, testpattern, f)
return inner
def reverse_blender(f):
def inner(b):
return bitmapfilter.blend(b, testpattern, b, f)
return inner
inverse_greyscale_weights = bitmapfilter.ChannelMixer(
1 - 0.299,
1 - 0.587,
1 - 0.114,
1 - 0.299,
1 - 0.587,
1 - 0.114,
1 - 0.299,
1 - 0.587,
1 - 0.114,
)
blur_more = [
4,
15,
24,
15,
4,
15,
61,
97,
61,
15,
24,
97,
154,
97,
24,
15,
61,
97,
61,
15,
4,
15,
24,
15,
4,
]
# "Sketch" filter based on
# https://www.freecodecamp.org/news/sketchify-turn-any-image-into-a-pencil-sketch-with-10-lines-of-code-cf67fa4f68ce/
def sketch(b):
bitmapfilter.mix(b, inverse_greyscale_weights)
memoryview(auxbuffer)[:] = memoryview(b)
bitmapfilter.morph(auxbuffer, blur_more)
bitmapfilter.blend(b, auxbuffer, b, color_dodge)
bitmapfilter.mix(b, inverse_greyscale_weights) # get rid of magenta halos
return b
effects = [
("sketch", sketch),
("50/50", blender(blend_50_50)),
("multiply", blender(multiply)),
("soft light", blender(soft_light)),
("hard_light", blender(hard_light)),
("blue cast", imageprocessing.blue_cast),
("blur", imageprocessing.blur),
("bright", lambda b: bitmapfilter.mix(b, bitmapfilter.ChannelScale(2.0, 2.0, 2.0))),
("emboss", imageprocessing.emboss),
("green cast", imageprocessing.green_cast),
("greyscale", imageprocessing.greyscale),
("ironbow", imageprocessing.ironbow),
(
"low contrast",
lambda b: bitmapfilter.mix(
b, bitmapfilter.ChannelScaleOffset(0.5, 0.5, 0.5, 0.5, 0.5, 0.5)
),
),
("negative", imageprocessing.negative),
("red cast", imageprocessing.red_cast),
("sepia", imageprocessing.sepia),
("sharpen", imageprocessing.sharpen),
("solarize", bitmapfilter.solarize),
(
"swap r/b",
lambda b: bitmapfilter.mix(
b, bitmapfilter.ChannelMixer(0, 0, 1, 0, 1, 0, 1, 0, 0)
),
),
]
def cycle(seq):
while True:
for s in seq:
yield s
effects_cycle = iter(cycle(effects))
DISPLAY_INTERVAL = 2000 # milliseconds
decoder = JpegDecoder()
pycam = PyCameraBase()
pycam.init_display()
testpattern = displayio.Bitmap(208, 208, 65535)
auxbuffer = displayio.Bitmap(208, 208, 65535)
def main():
filename = "/cornell_box_208x208.jpg"
bitmap = displayio.Bitmap(208, 208, 65535)
bitmap0 = displayio.Bitmap(208, 208, 65535)
decoder.open(filename)
decoder.decode(bitmap0)
decoder.open("/testpattern_208x208.jpg")
decoder.decode(testpattern)
label = Label(font=FONT, x=0, y=8)
pycam.display.root_group = label
pycam.display.refresh()
deadline = ticks_ms()
while True:
now = ticks_ms()
if pycam.up.fell:
deadline = now
if pycam.down.fell:
deadline = now
if pycam.left.fell:
deadline = now
if pycam.right.fell:
deadline = now
if ticks_less(deadline, now):
memoryview(bitmap)[:] = memoryview(bitmap0)
deadline = ticks_add(deadline, DISPLAY_INTERVAL)
effect_name, effect = next(effects_cycle) # random.choice(effects)
print(effect)
print(f"applying {effect=}")
t0 = ticks_ms()
effect(bitmap)
t1 = ticks_ms()
dt = ticks_diff(t1, t0)
print(f"{dt}ms to apply effect")
pycam.blit(bitmap, x_offset=16)
label.text = f"{dt:4}ms: {effect_name}"
pycam.display.refresh()
main()