将海龟图形保存到 .eps 文件时,背景颜色显示在屏幕上但未保存在 .eps 文件中 [英] When saving turtle graphics to an .eps file, the background color shows on the screen but is not saved in the .eps file

查看:42
本文介绍了将海龟图形保存到 .eps 文件时,背景颜色显示在屏幕上但未保存在 .eps 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,一直在使用乌龟模块作为学习语言的一种方式.

I am new to Python and have been working with the turtle module as a way of learning the language.

感谢 stackoverflow,我研究并学习了如何将图像复制到封装的 postscript 文件中,并且效果很好.然而,有一个问题.turtle 模块允许显示在屏幕上但不显示在 .eps 文件中的背景颜色.所有其他颜色,即钢笔颜色和海龟颜色,都可以通过,但背景颜色不可以.

Thanks to stackoverflow, I researched and learned how to copy the image into an encapsulated postscript file and it works great. There is one problem, however. The turtle module allows background color which shows on the screen but does not show in the .eps file. All other colors, i.e. pen color and turtle color, make it through but not the background color.

有趣的是,我不认为导入 Tkinter 是必要的,因为我不相信我在这里使用了任何 Tkinter 模块.我把它作为尝试诊断问题的一部分.我还使用了 bgcolor=Orange 而不是 s.bgcolor="orange".

As a matter of interest, I do not believe the import of Tkinter is necessary since I do not believe I am using any of the Tkinter module here. I included it as a part of trying to diagnose the problem. I had also used bgcolor=Orange rather than the s.bgcolor="orange".

不快乐.

我包含了一个简单的代码示例:

I am including a simple code example:

# Python 2.7.3 on a Mac

import turtle
from Tkinter import *

s=turtle.Screen()
s.bgcolor("orange")

bob = turtle.Turtle()
bob.circle(250)

ts=bob.getscreen()
ts.getcanvas().postscript(file = "turtle.eps")

<小时>

我尝试发布屏幕图像和 .eps 文件,但作为新用户,stackoverflow 不允许我这样做.某种垃圾邮件预防.虽然简单到可以可视化,但屏幕背景颜色为橙色,eps 文件为白色.


I tried to post the images of the screen and the .eps file but stackoverflow will not allow me to do so as a new user. Some sort of spam prevention. Simple enough to visualize though, screen has background color of orange and the eps file is white.

如果有任何想法,我将不胜感激.

I would appreciate any ideas.

推荐答案

Postscript 设计用于在纸张或胶片等介质上做标记,而不是光栅图形.因此,它本身没有可以设置为给定颜色的背景颜色,因为这通常是所用纸张或未曝光胶片的颜色.

Postscript was designed for making marks on some medium like paper or film, not raster graphics. As such it doesn't have a background color per se that can be set to given color because that would normally be the color of the paper or unexposed film being used.

为了模拟这一点,您需要绘制一个与画布大小相同的矩形,并用您想要的颜色作为背景填充它.我在turtle模块中没有看到任何内容来查询getcanvas()返回的canvas对象,我能想到的唯一替代方法是读取turtle.cfg文件,如果有的话,或者只是硬编码默认的 300x400 大小.您或许可以查看源代码并找出当前画布尺寸的存储位置并直接访问它们.

In order to simulate this you need to draw a rectangle the size of the canvas and fill it with the color you want as the background. I didn't see anything in the turtle module to query the canvas object returned by getcanvas() and the only alternative I can think of is to read the turtle.cfg file if there is one, or just hardcode the default 300x400 size. You might be able to look at the source and figure out where the dimensions of the current canvas are stored and access them directly.

更新:

我刚刚在 Python 控制台中使用 turtle 模块,并发现画布 getcanvas() 返回的内容有一个名为 _canvas<的私有属性/code> 这是一个 .该对象具有 winfo_width()winfo_height() 方法,它们似乎包含当前海龟图形窗口的尺寸.所以我会尝试绘制一个该大小的填充矩形,看看是否能满足您的需求.

I was just playing around in the Python console with the turtle module and discovered that what the canvas getcanvas() returns has a private attribute called _canvas which is a <Tkinter.Canvas instance>. This object has winfo_width() and winfo_height() methods which seem to contain the dimensions of the current turtle graphics window. So I would try drawing a filled rectangle of that size and see if that gives you what you want.

更新 2:

这里的代码显示了如何执行我的建议.注意:背景必须任何其他图形之前绘制,否则创建的实心填充背景矩形将覆盖屏幕上的所有其他内容.

Here's code showing how to do what I suggested. Note: The background must be drawn before any other graphics are because otherwise the solid filled background rectangle created will cover up everything else on the screen.

此外,添加的 draw_background() 函数会努力保存图形状态,然后将其恢复到原来的状态.根据您的具体使用情况,这可能不是必需的.

Also, the added draw_background() function makes an effort to save and later restore the graphics state to what it was. This may not be necessary depending on your exact usage case.

import turtle


def draw_background(a_turtle):
    """ Draw a background rectangle. """
    ts = a_turtle.getscreen()
    canvas = ts.getcanvas()
    height = ts.getcanvas()._canvas.winfo_height()
    width = ts.getcanvas()._canvas.winfo_width()

    turtleheading = a_turtle.heading()
    turtlespeed = a_turtle.speed()
    penposn = a_turtle.position()
    penstate = a_turtle.pen()

    a_turtle.penup()
    a_turtle.speed(0)  # fastest
    a_turtle.goto(-width/2-2, -height/2+3)
    a_turtle.fillcolor(turtle.Screen().bgcolor())
    a_turtle.begin_fill()
    a_turtle.setheading(0)
    a_turtle.forward(width)
    a_turtle.setheading(90)
    a_turtle.forward(height)
    a_turtle.setheading(180)
    a_turtle.forward(width)
    a_turtle.setheading(270)
    a_turtle.forward(height)
    a_turtle.end_fill()

    a_turtle.penup()
    a_turtle.setposition(*penposn)
    a_turtle.pen(penstate)
    a_turtle.setheading(turtleheading)
    a_turtle.speed(turtlespeed)

s = turtle.Screen()
s.bgcolor("orange")

bob = turtle.Turtle()
draw_background(bob)

ts = bob.getscreen()
canvas = ts.getcanvas()

bob.circle(250)

canvas.postscript(file="turtle.eps")

s.exitonclick()  # optional

这是产生的实际输出(通过 Photoshop 在屏幕上渲染):

And here's the actual output produced (rendered onscreen via Photoshop):

这篇关于将海龟图形保存到 .eps 文件时,背景颜色显示在屏幕上但未保存在 .eps 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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