【发布时间】:2020-10-15 02:44:23
【问题描述】:
我很好奇,想知道是否可以让 pyautogui 模块检测图像中每隔几个像素的颜色,并让海龟在画布上使用小圆圈重新创建它们。我最终找到了一种让它工作的方法,我喜欢图像的结果!我的代码唯一的问题是需要一段时间才能完成。根据图像的大小,大约需要 10-30 分钟才能完成。有什么办法可以优化我的代码吗?我还很新,一直在尝试不同的模块,所以我非常感谢任何建议或帮助!如果您需要我澄清任何事情,我也很高兴
import time
import turtle
import pyautogui
time.sleep(2)
minimum = pyautogui.position()
print(minimum)
time.sleep(2)
max_x, max_y = pyautogui.position()
print(max_x, max_y)#I use the first point as the top left corner, and the second as the bottom right.
wn = turtle.Screen()
t = turtle.Turtle()
wn.colormode(255)#Allows RGB colors
t.pensize(2)
t.speed(0)
wn.setup(width = 1.0, height = 1.0)
wn.bgcolor("black")
x, y = minimum
min_x, min_y = minimum
def turtlemove(x, y):
t.pu()
t.goto(x - 965, 565 - y)#Compared coordinates in pyautogui with positions in turtle so they match.
def circle():
t.pd()
t.begin_fill()
t.circle(2.9)
t.end_fill()
t.pu()
screenshot = pyautogui.screenshot()
while x != max_x and y != max_y:
coordinate = x, y
color = screenshot.getpixel(coordinate)
t.pencolor(color)
t.fillcolor(color)
turtlemove(x, y)
circle()
if x < max_x:
x += 6
else:
x = min_x
y += 6
#if y >= max_y:
#break
print(t.pos())
wn.exitonclick()
【问题讨论】:
-
你的代码不是太乱,但你应该看看 PEP8。另外,这是一个很酷的代码:)
-
我想知道你是否可以创建多个海龟,然后让它们并行绘制图像(使用多处理或线程)。
-
@Xilpex 哦,非常感谢!这意味着很多:D 我一定会查看 PEP8!
-
@jakub 哦,有趣的想法!
标签: python python-3.x turtle-graphics pyautogui python-turtle