Python 3乌龟(线只能通过画布中途呈现)

问题描述:

首先,我滥用乌龟模块。我有一个循环遍历目录中所有图像的小脚本。然后使用PIL读取它正在“扫描”的图像的每个像素的RGB值。它将龟变成相对于该像素的颜色,然后将龟移动到相对于图像的位置(在画布中)。实际上,我正在使用乌龟重新创建图像。Python 3乌龟(线只能通过画布中途呈现)

我对此稍后有更多的想法,但我一直遇到一个问题,即所有X像素都会在当前Y轴上呈现,直到下一个Y增量;那么前一行只能正确地在画布上中途呈现,而后一半则连续呈现一个像素的颜色。

Screenshot

依赖

  • PIL /枕头== 4.0.0
  • Canvasvg

注意 任何图像应该工作,只要图像托盘是RGBA。如果图像具有索引颜色托盘,则该脚本将废弃。

脚本

# Imports 
import os 
import time 
from PIL import Image 
import turtle 
import canvasvg 

wn = turtle.Screen() 
wn.tracer(0) 

pen = turtle.Turtle() 
pen.shape("square") 
pen.speed(0) 

# Load Image 
for image in os.listdir('image'): 
    # Create Empty Level 
    pixel = [] 
    # Open and process Image with PIL 
    im = Image.open("image/" + image) 
    width = im.size[0] 
    height = im.size[1] 
    wn.setup(width+50, height+50) 
    pix = im.load() 
    # Loop through the Y of the image 
    for y in range(height): 
     # Loop through the X of the image 
     pen.pendown() 
     for x in range(width): 
      # Get color of pixel, set color of turtle 
      color = pix[x,y] 
      pen.color((color[0]/255, color[1]/255, color[2]/255)) 
      # Move turtle 
      pen.goto(-(width/2) + x, (height/2) - y) 
      # Debug Info 
      print(x,y, pix[x,y]) 
     # Lift pen so there is not a streak across the window 
     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._canvas) 
    # Reset window for next image. 
    wn.reset() 

我看到你的代码的问题:

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以避免在内部循环中进行一些计算;使乌龟看不见,所以你不会浪费时间画它。

一般情况下我使用冲压,而不是绘制,对于这种在龟形象的工作,但我看你需要通过对canvasvg.saveall()工作图纸去做。

此外,这似乎是一个机会,使用setworldcoordinates()避免在Python级别进行坐标数学计算,但是当我尝试这样做时,我得到了精细的图像工件,因此我抛弃了它。

+0

我很欣赏这项工作。 Seem的我有点儿a and,只是看到了画布上的工件,并假定输出是相同的。也感谢您的修改。我的最终目标是处理来自reddit/r/place的最终图像,每个像素间隔4个像素,每个颜色都有一个单独的龟。 –

+0

@AaronHenderson,我认为这个神器会出现在输出文件中。即使我的解决方案修正了屏幕图像,也不是最佳的输出方式,因为它绘制了SVG中实际不需要的额外的完美水平回扫线。问题是正确绘制x == 0的情况。这是冲压得到的,但是这对你的目的不可行。 – cdlane