更新时间:2022-12-21 11:16:17
既然您提到您使用的是 Windows.您可以按照此答案中的指示使用 Win32 API
在 Windows 上使用 python 截取屏幕截图的最快方法.希望这会有所帮助.
但实际上 Pyscreenshot 应该是您要找的.p>
以下面的代码为例:
from pyscreenshot 导入抓取im = 抓取(bbox=(100, 200, 300, 400))im.show()
如您所见,您可以使用 bbox
截取坐标为 (100, 200) 且宽度为 300、高度为 400 的屏幕截图.
另外关于打印检查使用win32api打印.我希望这些有帮助.
使用 PIL 您可以调整大小:
from PIL import Image从 pyscreenshot 导入抓取img = 抓取(bbox=(100, 200, 300, 400))# 保持纵横比w = 300小时 = 400最大高度 = 600最大宽度 = 800比率 = 最小(最大宽度/宽度,最大高度/高度)# 正确的图像大小不是#oldsize * ratio## img.resize(...) 返回一个调整大小的图像并且不会影响 img 除非# 你分配返回值img = img.resize((h * ratio, width * ratio), Image.ANTIALIAS)
我建议更改您的程序,以便您可以在打印前调整图像大小
I need to do a screenshot of the content of the tkinter application below. I am on Windows 7 (or 8).
from Tkinter import *
def test(x):
#print "I'm in event:", x
if x == 1: # if event on entry e1
print 'e1 event' # do some thing
elif x == 2: # also if event on entry e2
print 'e2 event' # do some thing else
else:
print 'no event'
def test1(x):
test(1)
def test2(x):
test(2)
root=Tk()
root.minsize(500,500)
e1=Entry(root)
e1.pack()
e2=Entry(root)
e2.pack()
e1.bind( "<FocusOut>", test1)
e2.bind( "<FocusOut>", test2)
button=Button(root, text='print').pack(side=BOTTOM)
root.mainloop()
Since you mentioned that you are on Windows. You can use the Win32 API
as directed in this answer Fastest way to take a screenshot with python on windows. Hope this helps.
But actually Pyscreenshot should be what you are looking for.
Take the following code for example:
from pyscreenshot import grab
im = grab(bbox=(100, 200, 300, 400))
im.show()
As you can see you can use bbox
to take screenshot that is at co-ordinates (100, 200) and has a width of 300 and a height of 400.
Also as regards the printing check out Printing using win32api. I hope these help.
Using PIL you can do a resize:
from PIL import Image
from pyscreenshot import grab
img = grab(bbox=(100, 200, 300, 400))
# to keep the aspect ratio
w = 300
h = 400
maxheight = 600
maxwidth = 800
ratio = min(maxwidth/width, maxheight/height)
# correct image size is not #oldsize * ratio#
# img.resize(...) returns a resized image and does not effect img unless
# you assign the return value
img = img.resize((h * ratio, width * ratio), Image.ANTIALIAS)
I would advise changing your program so that you can resize the image before printing