-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvibrancy.py
95 lines (71 loc) · 2.71 KB
/
vibrancy.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pmma
import random
import time
import pygame
from PIL import Image, ImageFilter
import math
def display_to_string(surface, mode:str="RGBA") -> bytes:
surface_image = pygame.image.tostring(
surface,
mode)
return surface_image
def pygame_surface_to_pil_image(surface, mode:str="RGBA"):
return Image.frombytes(
mode,
surface.get_size(),
display_to_string(surface, mode=mode),
"raw")
def pil_image_to_pygame_surface(image, alpha=True):
surface = pygame.image.fromstring(
image.tobytes(),
image.size,
image.mode)
if alpha:
surface.convert_alpha()
if alpha is False:
surface.convert()
return surface
K = 20
HALF_K = K/2
def scaler(surface):
tempsurf = pygame.Surface((canvas.get_width(), canvas.get_height()))
surface = pygame.transform.smoothscale(surface, (canvas.get_width()+K, canvas.get_height()+K))
tempsurf.blit(surface, ((canvas.get_width()-surface.get_width())/2, (canvas.get_height()-surface.get_height())/2))
return tempsurf
canvas = pmma.Display()
canvas.create(1280, 720)
events = pmma.Events()
registry = pmma.Registry()
now_time = 0
start = time.perf_counter()
class Point:
def __init__(self, n):
self.noise_x = pmma.Perlin(random.randint(0, 999999))
self.noise_y = pmma.Perlin(random.randint(0, 999999))
self.noise_s = pmma.Perlin(random.randint(0, 999999))
self.noise_color = pmma.Perlin(random.randint(0, 999999))
self.n = n
def compute(self):
self.s = self.noise_s.generate_2D_perlin_noise(now_time/100, 0, [1, 10])
self.x = -40+(1920-math.sin(now_time + self.n)*self.noise_s.generate_2D_perlin_noise(0, now_time/10, [1, 500]))/2 # -40
self.y = -20+(1080-math.cos(now_time + self.n)*self.noise_s.generate_2D_perlin_noise(0, now_time/10, [1, 500]))/2 # -20
self.r = self.noise_color.generate_2D_perlin_noise(now_time, 0, [0, 255])
self.g = self.noise_color.generate_2D_perlin_noise(0, now_time, [0, 255])
self.b = self.noise_color.generate_2D_perlin_noise(now_time, now_time, [0, 255])
def render(self):
pygame.draw.circle(surface, (self.r, self.g, self.b), (self.x, self.y), self.s)
points = []
N = 20
for i in range(60):
points.append(Point(i))
surface = pygame.Surface((canvas.get_width(), canvas.get_height()))
while registry.running:
canvas.clear(0, 0, 0)
events.handle(canvas)
for point in points:
point.compute()
point.render()
surface = scaler(surface)
canvas.blit(surface, (0, 0))
canvas.refresh()
now_time = time.perf_counter()-start