-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle-draws.py
165 lines (133 loc) · 4.42 KB
/
turtle-draws.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
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
import turtle
import colorsys
import random
from PIL import Image
import io
import os
import time
# * Function to setup turtle.
def setup_turtle():
try:
# Initialize turtle variables.
t = turtle.Turtle()
screen = turtle.Screen()
# Initialize screen options.
screen.screensize(canvwidth=screen.window_width(),
canvheight=screen.window_height())
screen.bgcolor("black")
screen.setup(width=1.0, height=1.0)
# Set turtle boundaries to match canvas size.
left_bound = -screen.window_width() / 2
right_bound = screen.window_width() / 2
bottom_bound = -screen.window_height() / 2
top_bound = screen.window_height() / 2
turtle.setworldcoordinates(
left_bound, bottom_bound, right_bound, top_bound)
# Set other turtle properties.
t.color("white")
t.speed(0)
t.pensize(3)
return t, screen
except turtle.TurtleGraphicsError as te:
print(f"An error occurred while setting up the turtle: {te}")
except Exception as e:
print(f"An error occurred: {e}")
# Function to draw random circles.
def random_circle(t):
radius = random.randint(1, 100)
t.circle(radius)
# Function to draw random dots.
def random_dot(t):
size = random.randint(1, 20)
t.dot(size)
# Function to move in a random speed.
def random_speed(t):
speed = random.randint(0, 10)
t.speed(speed)
# * Function to draw random patterns.
def draw_random_patterns(t, iterations=1000):
commands = [t.fd, t.bk, t.rt, t.lt,
random_circle, random_dot, random_speed]
# Disable automatic updating of canvas.
turtle.tracer(0)
turtle.penup()
turtle.hideturtle()
n = 70
h = 0
for _ in range(0, random.randint(0, iterations)):
c = colorsys.hsv_to_rgb(h, 1, 0.8)
h += 1/n
for i in range(5):
try:
t.color(c)
t.width(i)
random_command = random.choice(commands)
execute_random_command(t, random_command)
turtle.update()
except turtle.TurtleGraphicsError as te:
print(f"Turtle Graphics Error: {te}")
except ValueError as ve:
print(f"Value Error: {ve}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# * Function to execute random turtle commands.
def execute_random_command(t, command):
try:
if command in [t.fd, t.bk, t.rt, t.lt]:
distance = random.randint(1, 200)
command(distance)
else:
command(t)
except Exception as e:
print(
f"An error occurred while executing the command {command.__name__}: {e}")
# * Function to Save the Image.
def save_image(screen):
timestamp = int(time.time())
image_folder = "./turtle-draws-img"
image_name = f"{image_folder}/turtle_art_{timestamp}.png"
# Ensure the image directory exists
if not os.path.exists(image_folder):
try:
os.makedirs(image_folder)
except OSError as e:
print(
f"Error occurred while creating the directory {image_folder}: {e}")
return
try:
canvas_ps = screen.getcanvas().postscript(colormode="color")
image = Image.open(io.BytesIO(canvas_ps.encode("UTF-8")))
image_rgb = image.convert("RGBA")
pixels = image_rgb.load()
width, height = image_rgb.size
for y in range(height):
for x in range(width):
if pixels[x, y] == (255, 255, 255, 255):
pixels[x, y] = (0, 0, 0, 255)
# Save the image.
image_rgb.save(image_name)
except IOError as e:
print(f"IOError occurred while saving the file: {e}")
except Exception as e:
print(f"Unexpected error occurred while saving the file: {e}")
# * Function to Close Turtle.
def close_turtle(screen):
# Close turtle screen
try:
screen.bye()
turtle.done()
except turtle.Terminator:
print("Turtle has already been terminated.")
except Exception as e:
print(f"Unexpected error encountered: {e}")
finally:
turtle.bye()
# * Main Function.
def main():
t, screen = setup_turtle()
draw_random_patterns(t)
save_image(screen)
close_turtle(screen)
# * Run Main Function.
if __name__ == "__main__":
main()