Python 如何将画布内容转换为图像?

Python 如何将画布内容转换为图像?,python,bitmap,tkinter,Python,Bitmap,Tkinter,我想将画布内容转换为位图或其他图像,然后执行其他操作,如旋转或缩放图像,或更改其坐标 如果我不再绘图,位图可以提高显示效率 我该怎么办?您可以生成一个postscript文档(输入其他工具:ImageMagick、Ghostscript等): 或者在PIL和Tkinter的画布上并行绘制相同的图像(请参见:)。例如(受同一篇文章的启发): 我找到了一个很好的方法来做这件事,这真的很有帮助。为此,您需要PIL模块。代码如下: from Tkinter import * import Image,

我想将画布内容转换为位图或其他图像,然后执行其他操作,如旋转或缩放图像,或更改其坐标

如果我不再绘图,位图可以提高显示效率


我该怎么办?

您可以生成一个postscript文档(输入其他工具:ImageMagick、Ghostscript等):

或者在PIL和Tkinter的画布上并行绘制相同的图像(请参见:)。例如(受同一篇文章的启发):


我找到了一个很好的方法来做这件事,这真的很有帮助。为此,您需要PIL模块。代码如下:

from Tkinter import *
import Image, ImageDraw

width = 400
height = 300
center = height//2
white = (255, 255, 255)
green = (0,128,0)

root = Tk()

# Tkinter create a canvas to draw on
cv = Canvas(root, width=width, height=height, bg='white')
cv.pack()

# PIL create an empty image and draw object to draw on
# memory only, not visible
image1 = Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image1)

# do the Tkinter canvas drawings (visible)
cv.create_line([0, center, width, center], fill='green')

# do the PIL image/draw (in memory) drawings
draw.line([0, center, width, center], green)

# PIL image can be saved as .png .jpg .gif or .bmp file (among others)
filename = "my_drawing.jpg"
image1.save(filename)

root.mainloop()
这样做的目的是将小部件名称传递到函数中。命令
root.winfo_rootx()
root.winfo_rooty()
获取整个
root
窗口左上角的像素位置

然后,添加
widget.winfo_x()
widget.winfo_y()
,基本上只需获取要捕获的小部件左上角像素的像素坐标(在屏幕的像素(x,y)处)

然后我找到(x1,y1),它是小部件的左下像素。
ImageGrab.grab()
生成一个打印屏幕,然后我将其裁剪为仅获取包含小部件的位。虽然不是完美的,也不会产生最好的图像,但这是一个很好的工具,用于获取任何小部件的图像并保存它


如果您有任何问题,请发表评论!希望这有帮助

使用枕头将Postscript转换为PNG

from PIL import ImageGrab

def getter(widget):
    x=root.winfo_rootx()+widget.winfo_x()
    y=root.winfo_rooty()+widget.winfo_y()
    x1=x+widget.winfo_width()
    y1=y+widget.winfo_height()
    ImageGrab.grab().crop((x,y,x1,y1)).save("file path here")

也许您可以尝试使用widget_winfo_id来获取画布的HWND

from PIL import Image

def save_as_png(canvas,fileName):
    # save postscipt image 
    canvas.postscript(file = fileName + '.eps') 
    # use PIL to convert to PNG 
    img = Image.open(fileName + '.eps') 
    img.save(fileName + '.png', 'png') 

在actionscript中,它有draw()函数来实现,如何在python tkinter中工作?嗨,当我运行代码时,它会使图片太快。你能发布一个带有tkinter图形的示例脚本,然后保存它吗?可能我在错误的时间放置或调用了该函数。@kerikmuuli您只能在需要拍照时调用该函数。例如,您可以使用命令获取按钮,在按钮命令的函数中,只需将
getter(x)
放在其中,x是小部件,甚至是整个根窗口。应该没问题。如果您还有其他问题,请在此处回复评论。@Ekikmuuli首先,您使用的是pyscreenshot模块而不是PIL,因此我不完全确定这是如何工作的。由于PIL可以进行屏幕截图和许多其他功能,因此我只需使用PIL模块即可。而且,看起来你只是想制作画布,然后他们制作一幅矩形的图片。有更简单的方法可以做到这一点!按照你的方式来做,然后使用PIL和我在回答中使用的save函数,如果你真的想在截图后退出,那么只需在getter函数定义中添加exit()或root.destroy()命令。这是一个了不起的方法!比尝试在空白PIL图像上重复所有绘图命令或创建Postscipt输出并使用其他工具将其转换为图像要好得多。请注意,不必捕获整个屏幕然后对其进行裁剪,而是可以使用
ImageGrab.grab((x,y,x1,y1)).save(“此处的文件路径”)
(请参阅文档)一步完成。还应指出,给定的文件扩展名决定了创建的图像文件的格式。
ImportError:ImageGrab仅适用于macOS和Windows
当我尝试转换为postscript时,它会调整图像的大小(保存的postscript相对于画布较小);为什么?既然postscript是矢量格式,我认为这不应该是个问题。在没有画布对象的情况下如何执行此操作?假设我有一个.ps文件以及将其转换为.png的内容?只需使用下面两行即可。当我尝试此操作时,我从PIL(Pillow)得到一个错误,即
OSError:无法在路径上找到Ghostscript
from PIL import ImageGrab

def getter(widget):
    x=root.winfo_rootx()+widget.winfo_x()
    y=root.winfo_rooty()+widget.winfo_y()
    x1=x+widget.winfo_width()
    y1=y+widget.winfo_height()
    ImageGrab.grab().crop((x,y,x1,y1)).save("file path here")
from PIL import Image

def save_as_png(canvas,fileName):
    # save postscipt image 
    canvas.postscript(file = fileName + '.eps') 
    # use PIL to convert to PNG 
    img = Image.open(fileName + '.eps') 
    img.save(fileName + '.png', 'png') 
import win32gui

from PIL import ImageGrab

HWND = canvas.winfo_id()  # get the handle of the canvas

rect = win32gui.GetWindowRect(HWND)  # get the coordinate of the canvas

im = ImageGrab.grab(rect)  # get image of the current location