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()
使用枕头将Postscript转换为PNG
我找到了一个很好的方法来做这件事,真的很有帮助。为此,您需要PIL模块。代码如下:
这样做的目的是向函数传递一个小部件名称。命令
root.winfo_rootx()
和root.winfo_rooty()
获取整个root
窗口左上角的像素位置。然后,将
widget.winfo_x()
和widget.winfo_y()
添加到中,基本上只需获得要捕获的小部件左上角像素的像素坐标(在屏幕的像素(x,y)处)。然后我找到(x1,y1),它是小部件左下角的像素。
ImageGrab.grab()
生成一个printscreen,然后我将其裁剪为只获取包含小部件的位。虽然不是完美的,也不会产生最好的图像,但这是一个很好的工具,只需获取任何小部件的图像并保存它。如果你有任何问题,请发表评论!希望这有帮助!
您可以生成postscript文档(以馈送到其他工具:ImageMagick、Ghostscript等):
或者在PIL和Tkinter的画布上并行绘制相同的图像(请参见:Saving a Tkinter Canvas Drawing (Python))。例如(受同一篇文章启发):
相关问题 更多 >
编程相关推荐