python截图保存到内存_python - 将画布从Tkinter保存到文件 - 堆栈内存溢出

以下是仅截取tkinter画布截图的代码。 PIL.ImageGrab模块在Linux中不起作用; 在Ubuntu 16.04中测试了此代码。 您可能必须检查它是否在Windows / OSx中运行。 请注意函数self._grabtofile 。

备注:在Ubuntu中,必须直接在命令行/终端上执行此脚本。 从IDLE中为python3执行时,它不起作用。

摘要:

能够显示tkinter画布的屏幕截图,并使用两个事件将其保存到文件a中。

可以对tkinter画布进行截图(不显示),并使用一个事件将其保存到文件中。

工作代码:

#!/usr/bin/python3

# -*- coding: utf-8 -*-

try:

import tkinter as tk # Python 3 tkinter modules

except ImportError:

import Tkinter as tk # Python 2 tkinter modules

from PIL import Image, ImageTk

#from PIL import Image, ImageTk, ImageGrab # For Windows & OSx

import pyscreenshot as ImageGrab # For Linux

class App(tk.Frame):

def __init__(self, parent):

tk.Frame.__init__(self, parent)

self.parent=parent

file = 'images.jpg'

self.img = Image.open(file)

#self.img.show() #Check to proof image can be read in and displayed correctly.

self.photo = ImageTk.PhotoImage(self.img)

print('size of self.img =', self.img.size)

centerx= self.img.size[0]//2

centery= self.img.size[1]//2

print ('center of self.img = ', centerx, centery)

self.cv = tk.Canvas(self)

self.cv.create_image(centerx, centery, image=self.photo)

self.cv.create_rectangle(centerx*0.5,centery*0.5,centerx*1.5,centery*1.5,

outline='blue')

self.cv.grid(row=0, column=0, columnspan=3, sticky='nsew')

self.snappic=tk.Button(self, text='SNAP', command=self._snapCanvas)

self.snappic.grid(row=1, column=0, sticky='nsew')

self.savepic=tk.Button(self, text='SAVE', command=self._save)

self.savepic.grid(row=1, column=1, sticky='nsew')

self.directsavepic=tk.Button(self, text='Grab_to_File', command=self._grabtofile)

self.directsavepic.grid(row=1, column=2, sticky='nsew')

self.snapsave=tk.Button(self, text='SNAP & SAVE', command=self._snapsaveCanvas)

self.snapsave.grid(row=2, column=0, columnspan=2, sticky='nsew')

def _snapCanvas(self):

print('\n def _snapCanvas(self):')

canvas = self._canvas() # Get Window Coordinates of Canvas

self.grabcanvas = ImageGrab.grab(bbox=canvas)

self.grabcanvas.show()

def _save(self):

self.grabcanvas.save("out.jpg")

print('Screenshoot of tkinter.Canvas saved in "out.jpg"')

def _grabtofile(self):

'''Remark: The intension was to directly save a screenshoot of the canvas in

"out_grabtofile.png".

Issue 1: Only a full screenshot was save.

Issue 2: Saved image format defaults to .png. Other format gave errors.

Issue 3: "ImageGrab.grab_to_file" only able to return full screenshoot

and not just the canvas. '''

print('\n def _grabtofile(self):')

canvas = self._canvas() # Get Window Coordinates of Canvas

print('canvas = ', canvas)

ImageGrab.grab_to_file("out_grabtofile.png", ImageGrab.grab(bbox=canvas))

print('Screenshoot of tkinter.Canvas directly saved in "out_grabtofile.png"')

def _snapsaveCanvas(self):

print('\n def _snapsaveCanvas(self):')

canvas = self._canvas() # Get Window Coordinates of Canvas

self.grabcanvas = ImageGrab.grab(bbox=canvas).save("out_snapsave.jpg")

print('Screencshot tkinter canvas and saved as "out_snapsave.jpg w/o displaying screenshoot."')

def _canvas(self):

print(' def _canvas(self):')

print('self.cv.winfo_rootx() = ', self.cv.winfo_rootx())

print('self.cv.winfo_rooty() = ', self.cv.winfo_rooty())

print('self.cv.winfo_x() =', self.cv.winfo_x())

print('self.cv.winfo_y() =', self.cv.winfo_y())

print('self.cv.winfo_width() =', self.cv.winfo_width())

print('self.cv.winfo_height() =', self.cv.winfo_height())

x=self.cv.winfo_rootx()+self.cv.winfo_x()

y=self.cv.winfo_rooty()+self.cv.winfo_y()

x1=x+self.cv.winfo_width()

y1=y+self.cv.winfo_height()

box=(x,y,x1,y1)

print('box = ', box)

return box

if __name__ == '__main__':

root = tk.Tk()

root.title('App'), root.geometry('300x300')

app = App(root)

app.grid(row=0, column=0, sticky='nsew')

root.rowconfigure(0, weight=1)

root.columnconfigure(0, weight=1)

app.rowconfigure(0, weight=10)

app.rowconfigure(1, weight=1)

app.columnconfigure(0, weight=1)

app.columnconfigure(1, weight=1)

app.columnconfigure(2, weight=1)

app.mainloop()

GUI屏幕截图:

GUI的tk.Canvas屏幕截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值