Python - turtle 重建图像太慢

标签 python python-3.x turtle-graphics pyautogui python-turtle

我很好奇,想知道是否可以让 pyautogui 模块从图像中每隔几个像素检测颜色,并让 turtle 使用小圆圈在 Canvas 中重新创建它们。我最终找到了一种让它工作的方法,我喜欢图像的结果!我的代码唯一的问题是它需要一段时间才能完成。根据图像的大小,大约需要 10-30 分钟才能完成。有什么办法可以优化我的代码吗?我还很新,一直在尝试不同的模块,所以我非常感谢任何建议或帮助!如果您需要我澄清任何事情,我也很高兴This took about 30 minutes?

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()

最佳答案

下面最显着的速度变化是使用 tracer()update()最小化 turtle 在屏幕上的移动。你会惊讶于差异。但是,我还更改了坐标的处理方式、点的绘制方式以及其他内容:

from time import sleep
from turtle import Screen, Turtle
from pyautogui import position, screenshot

DOT_DIAMETER = 6
CHROME = 14  # borders and other such overhead of turtle window

# Obtain the top left corner, and the bottom right:
print("Move mouse to upper left corner.")
sleep(2)
min_x, min_y = position()
print(min_x, min_y)

print("Move mouse to lower right corner.")
sleep(2)
max_x, max_y = position()
print(max_x, max_y)

screenshot = screenshot()

screen = Screen()
screen.setup(width=max_x - min_x + 1 + CHROME, height=max_y - min_y + 1 + CHROME)
screen.setworldcoordinates(min_x, max_y, max_x, min_y)
screen.bgcolor('black')
screen.colormode(255)
screen.tracer(False)

turtle = Turtle()
turtle.penup()

for y in range(min_y, max_y, DOT_DIAMETER):
    for x in range(min_x, max_x, DOT_DIAMETER):
        coordinate = x, y
        # on my system getpixel() returned four values
        *color, alpha = screenshot.getpixel(coordinate)
        turtle.goto(coordinate)
        turtle.dot(DOT_DIAMETER * 1.333, color)  # * 1.333 for color overlap

    screen.update()

screen.tracer(True)
screen.exitonclick()
我发现 screenshot.getpixel()在我的系统上返回四个值,但 turtle 颜色只有三个,所以我必须这样做:
*color, alpha = screenshot.getpixel(coordinate)
您可能需要在系统上改回它。
enter image description here

关于Python - turtle 重建图像太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62561854/

相关文章:

python - 更新 setuptools 后更新 pip,也可能更新发行版

python - imaplib python 与 ssl 证书

python - 通过python在字典内的字典中打印数字

python - 无法确定 turtle 运动的时间

python - 一组python的幂集和笛卡尔积

Python搁置OutOfMemory错误

python - kivy随机屏幕显示

python - 模拟 Popen 根据 call_args 返回不同的结果

python - 使用 Python 的 turtle 模块绘制此图案。一些正方形彼此重叠但倾斜有点像螺旋

python - 每次在 Python 中按下同一个按钮时,如何按顺序更改 turtle 图像?