-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path11.py
More file actions
65 lines (51 loc) · 1.64 KB
/
Copy path11.py
File metadata and controls
65 lines (51 loc) · 1.64 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
import turtle
import json
import time
def draw_from_json(json_file, pause=0.0):
screen = turtle.Screen()
screen.bgcolor("black")
screen.setup(800, 800)
screen.tracer(0)
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.penup()
t.pensize(0)
with open(json_file) as f:
regions = json.load(f)
all_points = [p for r in regions for p in r['contour']]
min_x = min(p[0] for p in all_points)
max_x = max(p[0] for p in all_points)
min_y = min(p[1] for p in all_points)
max_y = max(p[1] for p in all_points)
img_w = max_x - min_x
img_h = max_y - min_y
scale = min(700 / img_w, 700 / img_h)
cx = (min_x + max_x) / 2
cy = (min_y + max_y) / 2
def to_screen(px, py):
return (px - cx) * scale, (cy - py) * scale
total = len(regions)
print(f"Dibujando {total} regiones...")
for i, region in enumerate(regions):
r, g, b = [int(c) for c in region['color']]
color_hex = f'#{r:02x}{g:02x}{b:02x}'
t.color(color_hex, color_hex)
points = region['contour']
if len(points) < 3:
continue
sx, sy = to_screen(points[0][0], points[0][1])
t.goto(sx, sy)
t.begin_fill()
for pt in points[1:]:
t.goto(*to_screen(pt[0], pt[1]))
t.goto(sx, sy)
t.end_fill()
screen.update()
if pause > 0:
time.sleep(pause)
print(f" {i+1}/{total}", end='\r')
print(f"\nDibujo completado")
screen.mainloop()
if __name__ == "__main__":
draw_from_json("resources/tulipanes.json", pause=0.2)