-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomatic_gui.py
166 lines (129 loc) · 4.94 KB
/
automatic_gui.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
166
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image
import os
import pyperclip
def resize_and_reduce_colors(image_path, num_colors):
# Open the image
img = Image.open(image_path)
# Determine the aspect ratio of the image
width, height = img.size
aspect_ratio = width / height
# Calculate the desired width and height for resizing
target_height = 240
target_width = int(target_height * aspect_ratio)
# Resize the image while preserving aspect ratio
img = img.resize((target_width, target_height))
# Calculate the cropping dimensions
if target_width > 320:
crop_width = (target_width - 320) / 2
img = img.crop((crop_width, 0, target_width - crop_width, target_height))
elif target_height > 240:
crop_height = (target_height - 240) / 2
img = img.crop((0, crop_height, target_width, target_height - crop_height))
# Reduce the image to the selected number of colors
reduced_img = img.quantize(colors=num_colors)
return reduced_img
def generate_turtle_script(image_path, num_colors):
img = resize_and_reduce_colors(image_path, num_colors)
img = img.convert('RGB')
width, height = img.size
color_letters = {}
letter_counter = ord('a')
for x in range(width):
for y in range(height):
r, g, b = img.getpixel((x, y))
color = (r, g, b)
if color not in color_letters:
color_letters[color] = chr(letter_counter)
letter_counter += 1
color_string = ''
for y in range(height):
x = 0
while x < width:
color_count = 1
while x + color_count < width and img.getpixel((x + color_count, y)) == img.getpixel((x, y)):
color_count += 1
if color_count > 1:
color_string += str(color_count) + color_letters[img.getpixel((x, y))]
x += color_count
else:
color_string += color_letters[img.getpixel((x, y))]
x += 1
color_letters_mapping_str = '{\n'
for color, letter in color_letters.items():
color_letters_mapping_str += f" '{letter}': {list(color)},\n"
color_letters_mapping_str += '}'
turtle_script = f'''
from turtle import *
# Color letters mapping from the provided dictionary
color_letters_mapping = {color_letters_mapping_str}
width, height = {width}, {height}
speed(0)
penup()
hideturtle()
x, y = -width/2, height/2
goto(x, y)
color_string = '{color_string}'
# Draw based on color string
i = 0
while i < len(color_string):
char = color_string[i]
count = 0
# Accumulate digits to determine the length of line
while char.isdigit():
count = count * 10 + int(char)
i += 1
char = color_string[i]
if count == 0:
count = 1
# Get color RGB values
colort = color_letters_mapping[char]
i += 1
# Set turtle color
color(colort[0], colort[1], colort[2])
# Move turtle and draw line
pendown()
goto(x + count, y)
penup()
# Update x position for the next segment
x += count
# Check if x exceeds half of the width
if x >= width/2:
# Move to the next row
y -= 1
x = -width/2
goto(x, y)
'''
return turtle_script, len(color_letters)
def save_script(turtle_script):
save_path = filedialog.asksaveasfilename(defaultextension=".py", filetypes=[("Python files", "*.py")])
if save_path:
with open(save_path, 'w', encoding='utf-8') as file:
file.write(turtle_script)
messagebox.showinfo("Success", f"File saved as {os.path.basename(save_path)}")
def copy_to_clipboard(turtle_script):
pyperclip.copy(turtle_script)
messagebox.showinfo("Success", "Script copied to clipboard")
def select_image():
image_path = filedialog.askopenfilename(filetypes=[("Image files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
if image_path:
num_colors = color_slider.get()
turtle_script, _ = generate_turtle_script(image_path, num_colors)
colors_label.config(text=f"Selected number of colors: {num_colors}")
save_button.config(state=tk.NORMAL, command=lambda: save_script(turtle_script))
copy_button.config(state=tk.NORMAL, command=lambda: copy_to_clipboard(turtle_script))
root = tk.Tk()
root.title("Image to Turtle Drawing Script")
select_button = tk.Button(root, text="Select Image", command=select_image)
select_button.pack(pady=10)
colors_label = tk.Label(root, text="Selected number of colors: 10")
colors_label.pack(pady=10)
color_slider = tk.Scale(root, from_=5, to=50, orient=tk.HORIZONTAL, label="Number of Colors", length=200)
color_slider.set(10)
color_slider.pack(pady=10)
save_button = tk.Button(root, text="Save Python Script", state=tk.DISABLED)
save_button.pack(pady=10)
copy_button = tk.Button(root, text="Copy to Clipboard", state=tk.DISABLED)
copy_button.pack(pady=10)
root.mainloop()