使用Python图形模块:是否有办法将当前窗口保存为图像



我正在使用python图形模块。我要做的是将当前窗口保存为图像。在该模块中有一个将"image"保存为图像的选项(image.save())。但这没有帮助,因为它只是保存你已经加载的图像。或者,如果你像我一样加载一个空白图像,希望画在上面,它会改变,惊喜,惊喜:你得到一个空白图像保存。下面是我的代码:

from graphics import *

w = 300
h = 300
anchorpoint=Point(150,150)
height=300
width=300
image=Image(anchorpoint, height, width) #creates a blank image in the background
win = GraphWin("Red Circle", w, h)
# circle needs center x, y coordinates and radius
center = Point(150, 150)
radius = 80
circle = Circle(center, radius)
circle.setFill('red')
circle.setWidth(2)
circle.draw(win)
point= circle.getCenter()
print point
pointx= point.getX()
pointy= point.getY()
print pointx
print pointy
findPixel=image.getPixel(150,150)
print findPixel
image.save("blank.gif")
# wait, click mouse to go on/exit
win.getMouse()
win.close()
#######that's it#####

所以这又是我的问题:我如何保存现在屏幕上的内容为"blank.gif"谢谢!

您正在绘制的对象是基于Tkinter的。我不相信你实际上是在绘制基础图像,而是简单地通过使用"图形"库创建Tkinter对象。我也不相信你可以将Tkinter保存为"gif"文件,尽管你绝对可以将它们保存为postscript格式,然后将它们转换为gif格式。

为此,您将需要python的PIL库。

如果你所有的对象实际上都是TKinter对象,你可以简单地保存这些对象。

从替换这行代码开始:

image.save("blank.gif")

与以下内容:

# saves the current TKinter object in postscript format
win.postscript(file="image.eps", colormode='color')
# Convert from eps format to gif format using PIL
from PIL import Image as NewImage
img = NewImage.open("image.eps")
img.save("blank.gif", "gif")

如果您需要更多信息,请查看http://www.daniweb.com/software-development/python/code/216929 -这是我得到建议代码的地方。

我相信有比保存/转换更优雅的解决方案,但由于我对TKinter了解不多-这是我找到的唯一方法。

希望有帮助!

最新更新