【问题标题】:Working with the Python graphics module: is there any way to save the current window as an image?使用 Python 图形模块:有没有办法将当前窗口保存为图像?
【发布时间】:2012-12-28 03:13:17
【问题描述】:

我正在使用 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” 谢谢!

【问题讨论】:

    标签: python graphics


    【解决方案1】:

    您正在绘制的对象基于 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 了解不多 - 这是我找到的唯一方法。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-22
      • 2019-06-17
      • 2018-10-25
      • 2012-05-24
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多