Python 3 Turtle(线条仅在画布中间渲染)

Python 3 Turtle(线条仅在画布中间渲染),python,python-3.x,python-imaging-library,turtle-graphics,Python,Python 3.x,Python Imaging Library,Turtle Graphics,首先,我滥用了海龟模块。我有一个小脚本,可以循环遍历目录中的所有图像。然后使用PIL读取扫描图像的每个像素的RGB值。它将海龟更改为相对于该像素的颜色,然后将海龟移动到画布中相对于图像的位置。实际上,我正在使用turtle重新创建图像 稍后我会对此有更多想法,但我一直遇到一个问题,即所有X像素都将在当前Y轴上渲染,直到下一个Y增量;然后,上一行仅正确渲染画布的一半,下半行连续呈现一个像素颜色 依赖关系 PIL/枕头==4.0.0 拉票 注 只要图像托盘是RGBA,任何图像都应该工作。如果图像具有

首先,我滥用了海龟模块。我有一个小脚本,可以循环遍历目录中的所有图像。然后使用PIL读取扫描图像的每个像素的RGB值。它将海龟更改为相对于该像素的颜色,然后将海龟移动到画布中相对于图像的位置。实际上,我正在使用turtle重新创建图像

稍后我会对此有更多想法,但我一直遇到一个问题,即所有X像素都将在当前Y轴上渲染,直到下一个Y增量;然后,上一行仅正确渲染画布的一半,下半行连续呈现一个像素颜色

依赖关系

PIL/枕头==4.0.0 拉票 注 只要图像托盘是RGBA,任何图像都应该工作。如果图像具有索引颜色托盘,则脚本将报废

剧本


我发现您的代码有问题:

pen.goto(-(width/2) + x, (height/2) - y)
当代码从一行上的最后一个像素移动到下一行上的第一个像素时,该代码会创建回溯伪影-它将上一行的一半过度拉伸,可以将其视为反向的略微倾斜的线。您的penup&pendown无法解决此问题,因为在您返回到行的开头之前,笔已放下

我在下面重写了您的代码,并进行了一些更改/优化-查看此版本是否解决了您的问题:

# Imports
import os
import time
from turtle import Turtle, Screen
from PIL import Image
import canvasvg

BORDER = 25

wn = Screen()
wn.colormode(255)
wn.tracer(0)

pen = Turtle("square", visible=False)
pen.speed("fastest")
pen.penup() # Lift pen so there is no streak across the window

# Load Image
for image in os.listdir('image'):
    # Open and process Image with PIL
    im = Image.open("image/" + image)
    pix = im.load()

    width, height = im.size
    wn.setup(width + BORDER*2, height + BORDER*2)

    # Loop through the Y of the image
    for y in range(height):

        pen.sety(height/2 - y)
        pen.pendown()

        # Loop through the X of the image
        for x in range(width):
            # Get color of pixel, set color of turtle
            pen.color(pix[x, y])
            # Move turtle
            pen.setx(x - width/2)
            # Debug Info
            # print(x, y, pix[x, y])

        pen.penup()
        # Update the window
        wn.update()
        # Add delay so computer doesn't crap pants
        time.sleep(1)

    # Save canvas to SVG file
    canvasvg.saveall(image + ".svg", wn.getcanvas())
    # Reset window for next image.
    wn.reset()

wn.mainloop()
更改包括:设置颜色模式以匹配颜色样本以避免分割;分别移动笔的x和y,以避免在内部循环中进行某些计算;让乌龟隐形,这样你就不会浪费时间画它了

通常我会在turtle中使用戳记,而不是绘图,但我认为您需要通过绘图来完成canvasvg.saveall的工作


此外,这似乎是一个使用setworldcoordinates避免在Python级别进行坐标计算的机会,但当我尝试这样做时,我得到了一些细微的图像瑕疵,因此我将其扔掉。

我很欣赏这项工作。似乎我有点不安,只是看到画布上的工件,并假设输出是相同的。也感谢您的修改。我的最终目标是处理reddit/r/place的最终图像,每个像素间隔4个像素,每种颜色有一个单独的乌龟。@AaronHenderson,我认为工件应该在输出文件中。即使是我的解决方案,即修复屏幕图像,也不是最佳的输出方式,因为它绘制了一条额外完美的水平回溯线,这在SVG中并不需要。问题在于正确绘制了x==0的情况。这就是stamping所能做到的,但这对于你的目的来说是不可行的。
# Imports
import os
import time
from turtle import Turtle, Screen
from PIL import Image
import canvasvg

BORDER = 25

wn = Screen()
wn.colormode(255)
wn.tracer(0)

pen = Turtle("square", visible=False)
pen.speed("fastest")
pen.penup() # Lift pen so there is no streak across the window

# Load Image
for image in os.listdir('image'):
    # Open and process Image with PIL
    im = Image.open("image/" + image)
    pix = im.load()

    width, height = im.size
    wn.setup(width + BORDER*2, height + BORDER*2)

    # Loop through the Y of the image
    for y in range(height):

        pen.sety(height/2 - y)
        pen.pendown()

        # Loop through the X of the image
        for x in range(width):
            # Get color of pixel, set color of turtle
            pen.color(pix[x, y])
            # Move turtle
            pen.setx(x - width/2)
            # Debug Info
            # print(x, y, pix[x, y])

        pen.penup()
        # Update the window
        wn.update()
        # Add delay so computer doesn't crap pants
        time.sleep(1)

    # Save canvas to SVG file
    canvasvg.saveall(image + ".svg", wn.getcanvas())
    # Reset window for next image.
    wn.reset()

wn.mainloop()