-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path05-pixels.py
36 lines (30 loc) · 1.32 KB
/
05-pixels.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
from nodeboxgl.graphics import *
# The pixels() command yields a list of pixels from a given image.
# Since this is a relatively slow operation, this is not useful for dynamic image processing,
# but there a various other ways in which pixels can be useful. In short:
# - len(Pixels): the number of pixels in the image.
# - Pixels.width: the width of the image.
# - Pixels.height: the height of the image.
# - Pixels[i]: a list of [R,G,B,A] values between 0-255 that can be modified.
# - Pixels.get(x, y): returns a Color from the pixel at row x and column y.
# - Pixels.set(x, y, clr): sets the color of the pixel at (x,y).
# - Pixels.update() commits all the changes. You can then pass Pixels to the image() command.
img = Image("creature.png")
p = Pixels(img)
def draw(canvas):
# Since the background is a bit transparent,
# it takes some time for the previous frame to fade away.
background(1,0.03)
# Here we simply use pixels from the image as a color palette.
for i in range(15):
x = int(random(p.width))
y = int(random(p.height))
clr = p.get(x, y)
clr.alpha *= 0.5
fill(clr)
stroke(clr)
strokewidth(random(5))
r = random(5, 100)
ellipse(random(canvas.width), random(canvas.height), r*2, r*2)
canvas.size = 500, 500
canvas.run(draw)