python中如何让turtle在制图中间写字_Python3 Turtle(线条仅在画布中间渲染)

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

稍后我会有更多的想法,但是我一直遇到这样一个问题:所有X像素将在当前Y轴上渲染,直到下一个Y增量;然后前一行只正确地渲染画布的一半,后半部分是连续的一个像素颜色。在

3770f8ef4765f46128497f7915d2e8ed.png

依赖关系PIL/枕头==4.0.0

拉票

注意

只要图像托盘是长的。如果图像有一个索引的颜色托盘,脚本将崩溃。在

脚本# 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()


版权声明:本文为weixin_36310597原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。