如何与海龟画布/屏幕大小正确交互? [英] How to properly interact with turtle canvas/screen sizing?

查看:39
本文介绍了如何与海龟画布/屏幕大小正确交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python Turtle 绘制一些图像,但我无法调整屏幕大小/画布大小.

I am attempting to draw some images using Python Turtle and the screen sizing/canvas sizing is eluding me.

这是我必须创建的当前图像:

Here is what I have to create the current image:

import turtle
import math
from PIL import Image
import os

t = turtle.Turtle()

Image.MAX_IMAGE_PIXELS = None

screen = turtle.Screen()
fileName = "test"
seedString = fileName

turtle.setup(int(15000),int(15000),int(7500),int(7500))
turtle.screensize(int(15000), int(15000))
turtle.hideturtle()
screen.tracer(False)

t.pen(pensize=2, pencolor='black')

print(screen.screensize())
print(t.pos())

t.color('black')
t.begin_fill()
t.goto(7500,0)
t.right(90)
t.forward(7500)
t.right(90)
t.forward(15000)
t.right(90)
t.forward(15000)
t.right(90)
t.forward(15000)
t.right(90)
t.forward(7500)
t.end_fill()

print(t.pos())

t.goto(0,0)
t.pen(pencolor='white', pensize=5)
t.circle(4000, 360)

print(t.pos())
print(screen.screensize())

canvas = screen.getcanvas()
canvas.postscript(file=fileName, height=15000, width=15000)

img = Image.open(fileName)
img.save(seedString+'.jpg')
img.close()

os.system('move \"'+fileName+'\" somefilelocation')
os.system('move \"'+seedString+'.jpg\" somefilelocation')

根据我的理解,此脚本应将屏幕大小设置为 15,000 x 15,000 像素,并使用turtle.setup() 将原点移动到 (7,500,7,500) 的中心.我不确定这是画布还是我们看到海龟在其中移动的窗口.但我还包含了turtle.screensize() 函数将其设置为 15000 x 15000.也许这是多余的,因为它调整了同样的事情,但文档对此有点含糊.

Based on my understanding this script should set the Screen size to 15,000 by 15,000 pixels and move the origin to the center at (7,500,7,500) using turtle.setup(). I'm not sure if this is the canvas or just the window that we see the turtle move around in. But I also included the turtle.screensize() function to set it to 15000 x 15000. Perhaps this is redundant as it sizes the same thing, but the documentation is sort of vague regarding it.

最后我保存了一个同样大小为 15000 x 15000 像素的 PS 文件,但这个图像是创建的:

At the end I save a PS file with the same size of 15000 x 15000 pixels but this image is what is created:

边框是我加的,是为了让图片的实际尺寸更清晰.

The border was added by me to make the actual size of the image more viewable.

我难以理解的是一些事情.一个如果 setup() 和 screensize() 函数实际上只是改变窗口大小本身,那么我如何更改创建绘图的实际画布的大小.其次,我首先手动绘制一个矩形,该矩形由上面的 goto() 和 right() 函数组合填充为黑色.但是图像本身(这篇文章中的图像是为了大小而缩放的)被保存为 11251 x 11251.这不仅对我没有意义,因为我将保存的 PS 文件设置为 15000 x 15000 但即使保存后的原始图像是 11251 x 11251 那么为什么我的矩形,根据我编写它的方式应该是 15000 x 15000,比那个小得多.此外,原点不会出现在黑色矩形或保存的整个画布/图像中心附近的任何位置.

What I'm struggling to understand is a few things. One if the setup() and screensize() function is actually just changing the window size itself then how do I change the size of the actual canvas the drawing is created on. Second I start by manually drawing a rectangle that is filled black by the goto() and right() combination of functions above. But the image itself (the one in this post is scaled for the sake of size) is saved as 11251 x 11251. This does not only make no sense to me as I am setting the saved PS file to 15000 x 15000 but even if the original image was 11251 x 11251 after saving then why is my rectangle, which should be 15000 x 15000 based on how I scripted it, much much smaller than that. Also the origin does not appear anywhere near the center of either the black rectangle or the overall canvas/image that is saved.

最后,尽管我将范围参数明确设置为 360,但我还是不明白为什么 circle() 函数只产生一个半圆.我将它保留为 0,默认为一个完整的圆,它产生了相同的半圆.

Lastly I cannot understand why the circle() function is only producing a half circle despite me explicitly setting the extent argument to 360. I left it at 0 which is defaulted to a full circle and it produced the same semi circle.

我试过只包括设置或屏幕大小,使用绝对值,如 1.0 与像素值,但似乎没有任何东西能满足我的需求.这是一个 15000 x 15000 像素的图像,从原点画一个圆圈,画布的中心在 (7500,7500).我知道这不会是圆本身的中心,因为它从边缘开始并从该点到其上方或下方的点(半径,0)创建一个圆.但这不是我的脚本目前的工作方式.

I've tried only including setup or screensize, using absolute values like 1.0 vs pixel values and nothing seems to get me what I want. Which is a 15000 x 15000 pixel image with a circle drawn from the origin, being the center of the canvas at (7500,7500). I know that wouldn't be the center of the circle itself as it starts on an edge and creates a circle from that point to a point above or below it (radius,0) away. But that's not how my script is currently working.

很明显,我没有掌握一个基本的理解.任何帮助将不胜感激,因为我很想了解有关此库的更多信息并更好地了解画布/坐标系以更准确地创建图像.

It is clear that there is a fundamental understanding that I am not grasping. Any assistance would be much appreciated as I'd love to learn more about this library and better understand the canvas/coordinate system to more accurately create images.

谢谢!

推荐答案

您的逻辑存在一些缺陷.首先,您在完成绘制后调用 tracer(False) 但从不调用 screen.update(),这就是您的圆圈不完整的原因.

You've a couple of flaws in your logic. First, you call tracer(False) but never call screen.update() when you finish drawing, which is why your circle is incomplete.

第二,尽管海龟在 tkinter 上爬行,但它使用的默认坐标系与 Canvas 不同.所以我们需要在我们的 Canvas.postscript() 方法调用中进行调整:

Second, although turtle crawls atop tkinter, it uses a different default coordinate system than Canvas. So we need to adjust for that in our Canvas.postscript() method invocation:

from turtle import Turtle, Screen

WIDTH, HEIGHT = 15_000, 15_000

fileName = "test"

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.pensize(5)
turtle.penup()

turtle.goto(-WIDTH/2, -HEIGHT/2)

turtle.begin_fill()

for _ in range(2):
    turtle.forward(WIDTH)
    turtle.left(90)
    turtle.forward(HEIGHT)
    turtle.left(90)

turtle.end_fill()

turtle.goto(0, -HEIGHT/4)
turtle.pencolor('white')
turtle.pendown()
turtle.circle(HEIGHT/4)

screen.update()

canvas = screen.getcanvas()
canvas.postscript(file=fileName + ".eps", width=WIDTH, height=HEIGHT, x=-WIDTH/2, y=-HEIGHT/2)

您会注意到图像的宽度/高度(上面未完全显示)为 200 英寸,相当于 15_000 点的英寸(使用每英寸 75 点而不是 72 点.)

You'll note that the image width/height (not fully shown above) is 200 inches, which is the inch equivalent of 15_000 points (using 75 points per inch instead of 72.)

我将转换为 JPG 的最后一步留给您.

I leave the final step of conversion to JPG to you.

这篇关于如何与海龟画布/屏幕大小正确交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆