【问题标题】:Python turtle.setup() truncates canvas to screen size - how to avoid that?Python turtle.setup() 将画布截断为屏幕大小 - 如何避免这种情况?
【发布时间】:2013-11-12 01:25:19
【问题描述】:

要测量一根杆的旋转速度,我需要制作一个表盘,其中有大量交替排列的暗/透明段排成一圈。旋转表盘会中断光电传感器上的光线,然后我只需要测量光脉冲的频率即可。 Python 海龟图形似乎是绘制这个刻度盘的好主意。

我需要把这张图片画得很大,以避免分段边缘的阶梯效应——我需要平滑的边缘。但是,如果我使用大于屏幕的 x 或 y 执行 turtle.setup(x, y),则画布会被截断以适应屏幕。如何避免?

我的代码包含在最后。在此处查看截断画布的屏幕截图,其中 x = y = 1420

编辑: 只是为了说明清楚 - 最后的 getscreen() / getcanvas() 捕获此截断的画布图像并将其按原样(截断)保存到 EPS 文件中。这就是困扰我的地方。我需要在高分辨率图像文件中捕获整个圆圈。

我在 Ubuntu 13.04 上使用 python-2.7.4

这是代码:

#!/usr/bin/python

# set this to 1 to troubleshoot
debug = 0

import turtle
import math

# image file with the result
fname="dial.eps"

# number of lines
n = 100
# external radius
r2 = 700
# length of each line
l = round(r2 / 10)

r1 = r2 - l

# pen thickness
# tuned for 50% fill factor at the inner end of each line
# (dark stripe and transparent stripe have equal width there)
thick = 2 * math.pi * r1 / float(2 * n)
print "thickness =", thick

# setup screen size to contain the whole circle, plus a little extra
border = 20 + thick
turtle.setup(2 * r2 + border, 2 * r2 + border)

dot = turtle.Turtle()
dot.speed(0)
dot.hideturtle()

# draw crosshairs in the center
dot.setpos(l, 0)
dot.setpos(-l, 0)
dot.home()
dot.setpos(0, l)
dot.setpos(0, -l)
dot.penup()

# thickness of lines
dot.pensize(thick)

for step in range(0, n):
    a = 360.0 * step / float(n)
    arad = math.radians(a)
    x1 = r1 * math.cos(arad)
    y1 = r1 * math.sin(arad)
    x2 = r2 * math.cos(arad)
    y2 = r2 * math.sin(arad)
    if debug == 1:
        print "a =", a, "\t x1 =", x1, "\t y1 =", y1, "\t x2 =", x2, "\t y2 =", y2
    dot.penup()
    dot.setpos(x1, y1)
    dot.pendown()
    dot.setpos(x2, y2)

ts = turtle.getscreen()
ts.getcanvas().postscript(file=fname)

print "Saved image to: ", fname
print "All done. Click image to exit."

turtle.exitonclick()

【问题讨论】:

  • 打印ts.getcanvas()._canvas.winfo_height() & ts.getcanvas()._canvas.winfo_width() 来验证秤?而且,您可以使用this question's code,检查它是否有效。 postscript 从未设计用于存储光栅图像。
  • 画布宽度:2046 画布高度:933 应该都是 2046。看起来垂直被截断了,因为它比我的屏幕大。
  • 尝试使用上述网址中提到的tkinter.photoimage。也许它会存储整个图像。

标签: python turtle-graphics


【解决方案1】:
  • 您不能将海龟画布设置为比您的屏幕大
  • getcanvas().postscript保存的EPS文件与分辨率无关;您可以以任何尺寸打印它,它仍然会以您打印机的原始分辨率输出。

我修改了您的代码以读取屏幕大小并相应地更改圆半径:

#!/usr/bin/python

# set this to 1 to troubleshoot

debug = 0

import turtle
import math
ts = turtle.getscreen()
max_size = 0
if ts.window_width > ts.window_height:
    max_size = ts.window_height()
else:
    max_size = ts.window_width()

# image file with the result

fname = 'dial.eps'

# number of lines

n = 100

# external radius
# r2 = 700

r2 = 0.8 * max_size / 2

# length of each line - changed from 'l', which looks too much like 1

line_length = round(r2 / 10)

r1 = r2 - line_length

# pen thickness
# tuned for 50% fill factor at the inner end of each line
# (dark stripe and transparent stripe have equal width there)

thick = 2 * math.pi * r1 / float(2 * n)
print 'thickness =', thick

# setup screen size to contain the whole circle, plus a little extra

border = 20 + thick

# turtle.setup(2 * r2 + border, 2 * r2 + border)

dot = turtle.Turtle()
dot.speed(0)
dot.hideturtle()

# draw crosshairs in the center

dot.setpos(line_length, 0)
dot.setpos(-line_length, 0)
dot.home()
dot.setpos(0, line_length)
dot.setpos(0, -line_length)
dot.penup()

# thickness of lines

dot.pensize(thick)

for step in range(0, n):
    a = 360.0 * step / float(n)
    arad = math.radians(a)
    x1 = r1 * math.cos(arad)
    y1 = r1 * math.sin(arad)
    x2 = r2 * math.cos(arad)
    y2 = r2 * math.sin(arad)
    if debug == 1:
        print 'a =', a, '\t x1 =', x1, '\t y1 =', y1, '\t x2 =', x2, \
            '\t y2 =', y2
    dot.penup()
    dot.setpos(x1, y1)
    dot.pendown()
    dot.setpos(x2, y2)

ts.getcanvas().postscript(file=fname)

print 'Saved image to: ', fname
print 'All done. Click image to exit.'

turtle.exitonclick()

放大到 500%,没有锯齿:

哦,请不要使用l 作为变量名;它看起来太像1。 糟糕的程序员,没有饼干......☺

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多