When saving turtle graphics to an .eps file, the background color shows on the screen but is not saved in the .eps file

寵の児 提交于 2019-12-23 10:39:04

问题


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

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.

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".

No Joy.

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")

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.


回答1:


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.

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.

Update:

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.

Update 2:

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

Also, the added draw_background() function goes to a fair amount of 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
from Tkinter import *

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 = bob.heading()
    turtlespeed = bob.speed()
    penposn = bob.position()
    penstate = bob.pen()

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

    bob.penup()
    bob.setposition(*penposn)
    bob.pen(penstate)
    bob.setheading(turtleheading)
    bob.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

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




回答2:


I haven't found a way to get the canvas background colour on the generated (Encapsulated) PostScript file (I suspect it isn't possible). You can however fill your circle with a colour, and then use Canvas.postscript(colormode='color') as suggested by @mgilson:

import turtle

bob = turtle.Turtle()
bob.fillcolor('orange')
bob.begin_fill()
bob.circle(250)
bob.begin_fill()

ts = bob.getscreen()
ts.getcanvas().postscript(file='turtle.eps', colormode='color')


来源:https://stackoverflow.com/questions/13537483/when-saving-turtle-graphics-to-an-eps-file-the-background-color-shows-on-the-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!