-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (67 loc) · 2.83 KB
/
main.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
import turtle as t
from PIL import Image
from turtle import Screen
from typing import Tuple
class ImageProcessor:
"""Base class for image processing."""
def __init__(self, image_path: str):
self.image_path = image_path
self.img = Image.open(self.image_path)
self.rgb_img = self.img.convert("RGB")
self.width, self.height = self.img.size
def process_image(self):
raise NotImplementedError("This method should be overridden by subclasses.")
class TurtleImageProcessor(ImageProcessor):
"""Image processor that draws an image using turtle graphics."""
def __init__(self, image_path: str):
super().__init__(image_path)
self.screen = Screen()
self.screen.colormode(255)
self.offset_x, self.offset_y = self._set_initial_turtle_position()
t.shape("classic")
t.speed(0)
t.tracer(0, 0) # Disable turtle animation for faster drawing
def _set_initial_turtle_position(self) -> Tuple[float, float]:
"""Calculate the initial turtle position and set it."""
offset_x = -(self.width / 2)
offset_y = self.height / 2
t.penup()
t.setposition(offset_x, offset_y)
t.pendown()
return offset_x, offset_y
def process_image(self) -> None:
"""Draw the image using the turtle graphics."""
for y in range(self.height):
t.penup()
t.setposition(self.offset_x, -y + self.offset_y)
t.pendown()
row_colors = [self.rgb_img.getpixel((x, y)) for x in range(self.width)]
t.penup()
t.setposition(self.offset_x, -y + self.offset_y)
t.pendown()
for x, color in enumerate(row_colors):
t.pencolor(color)
t.setx(x + self.offset_x)
t.penup()
if y % 10 == 0: # Update the screen every 10 lines for better performance
t.update()
print(f"Current line: {y + 1} of {self.height}")
t.update() # Final screen update
print("Drawing finished")
t.done() # Finish the turtle graphics
class ImageProcessorFactory:
"""Factory class to create image processors."""
@staticmethod
def create_image_processor(processor_type: str, image_path: str) -> ImageProcessor:
if processor_type == 'turtle':
return TurtleImageProcessor(image_path)
else:
raise ValueError(f"Unknown processor type: {processor_type}")
def main() -> None:
"""Main function to execute the image processing."""
image_path = input("Please provide the image path: ")
processor_type = 'turtle' # Can be extended to support different types
processor = ImageProcessorFactory.create_image_processor(processor_type, image_path)
processor.process_image()
if __name__ == "__main__":
main()