【问题标题】:Python - Turtle recreating image too slowPython - 海龟重建图像太慢
【发布时间】: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


【解决方案1】:

以下是三个主要提示:

  1. 避免使用 + 运算符连接字符串
  2. 使用地图功能
  3. 避免重新评估函数

https://towardsdatascience.com/3-techniques-to-make-your-python-code-faster-193ffab5eb36[Read此博客以获取更多信息。][1]

【讨论】:

  • 给定的代码似乎没有任何这些问题
【解决方案2】:

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

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() 在我的系统上返回了四个值,但海龟颜色只有三个,所以我不得不这样做:

*color, alpha = screenshot.getpixel(coordinate)

您可能需要在系统上进行更改。

【讨论】:

  • 这太棒了!非常感谢,我完全不知道 tracer() 和 update(),现在它运行得更快了!感谢您花时间帮助我!有个美好的一天! :D
猜你喜欢
  • 2020-03-02
  • 2014-10-21
  • 2014-03-18
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
相关资源
最近更新 更多