-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCanvas.py
More file actions
57 lines (47 loc) · 1.45 KB
/
Copy pathCanvas.py
File metadata and controls
57 lines (47 loc) · 1.45 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
from PIL import Image
from typing import Tuple
import random
RGB = Tuple[int, int, int]
Pos = Tuple[int, int]
class Canvas:
def __init__(self, x, y):
# self.pixels[y, x] = pixel at y, x
# |---------------------> + x
# |
# v
# + y
self.pixels = [
[
(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for _ in range(x)
]
for _ in range(y)
]
self.age = 0
def read(self, startX, startY, width, height):
result = []
for y in range(startY, startY + height):
if y < 0 or y >= len(self.pixels):
continue
row = []
for x in range(startX, startX + width):
if x < 0 or x >= len(self.pixels[0]):
continue
row.append(self.pixels[y][x])
if row:
result.append(row)
return result
def write(self, x, y, col: RGB):
self.pixels[y][x] = col
def export(self, path="output.png"):
height = len(self.pixels)
width = len(self.pixels[0])
print(f"canvas age: {self.age}")
img = Image.new("RGB", (width, height))
img.putdata([pixel for row in self.pixels for pixel in row])
img.save(path)
print(f"image created: {path}")
def getAge(self):
return self.age
def increment_age(self):
self.age += 1