python - 使用 Python 图形模块 : is there any way to save the current window as an image?

标签 python graphics

我正在使用 python graphics模块。我想要做的是将当前窗口保存为图像。在该模块中,有一个选项可以将“图像”另存为图像 (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 了解不多 - 这是我找到的唯一方法。

希望对您有所帮助!

关于python - 使用 Python 图形模块 : is there any way to save the current window as an image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8530524/

相关文章:

python - 将每一行除以前一行

c++ - 将线剪裁到屏幕坐标

c++ - 在程序中包含图形界面

math - 求解向量的原点

德尔福/GDI+ : When is a Device Context created/destroyed?

python - 如何将 request.data 转换为 dict?

python - 开头的特殊字符与每个单词的结尾匹配[仅限正则表达式]

python - 如何从 html 表格元素中解析文本

python - 如何从 Pandas 的时间序列数据中获取斜率?

windows - 在 Windows 中生成专业图表