python imagegrab_如何在python的imagegrab库中为区域选择提供动态值

Using this script i am trying to take a screenshot of my desktop of a particular area.(using Tkinter gui)

But with this code i can only take screenshot of the fix area (frame) of desktop. So what i want to do is try to set the value of (bbox of imagegrab) dynamic.

And by dynamic i mean it should only capture the screen area which is selected(highlighted) by my mouse cursor any where on screen and can be of any size.

import tkinter as tk

from tkinter import *

from PIL import Image, ImageGrab

root = tk.Tk()

def area_sel():

# using the grab method

img = ImageGrab.grab(bbox = (400,500,400,500)) #i want these values to be dynamic

img.show()

sel_btn = tk.Button(root, text='select area', width=20, command=area_sel)

sel_btn.pack()

root.mainloop()

example image

here is what i am trying to do is to take coordinates from your code and then recording that particular area of screen.

def recording_screen():

global recording

recording = True

while recording:

sel=area_sel()

img = ImageGrab.grab(bbox=(Click_x, Click_y, Release_x, Release_y))

frame = np.array(img)

out.write(frame)

解决方案

You can use Pillow to do what you want:

import tkinter as tk

from PIL import Image, ImageTk, ImageGrab, ImageEnhance

root = tk.Tk()

root.resizable(0, 0)

def show_image(image):

win = tk.Toplevel()

win.image = ImageTk.PhotoImage(image)

tk.Label(win, image=win.image).pack()

win.grab_set()

win.wait_window(win)

def area_sel():

x1 = y1 = x2 = y2 = 0

roi_image = None

def on_mouse_down(event):

nonlocal x1, y1

x1, y1 = event.x, event.y

canvas.create_rectangle(x1, y1, x1, y1, outline='red', tag='roi')

def on_mouse_move(event):

nonlocal roi_image, x2, y2

x2, y2 = event.x, event.y

canvas.delete('roi-image') # remove old overlay image

roi_image = image.crop((x1, y1, x2, y2)) # get the image of selected region

canvas.image = ImageTk.PhotoImage(roi_image)

canvas.create_image(x1, y1, image=canvas.image, tag=('roi-image'), anchor='nw')

canvas.coords('roi', x1, y1, x2, y2)

# make sure the select rectangle is on top of the overlay image

canvas.lift('roi')

root.withdraw() # hide the root window

image = ImageGrab.grab() # grab the fullscreen as select region background

bgimage = ImageEnhance.Brightness(image).enhance(0.3) # darken the capture image

# create a fullscreen window to perform the select region action

win = tk.Toplevel()

win.attributes('-fullscreen', 1)

win.attributes('-topmost', 1)

canvas = tk.Canvas(win, highlightthickness=0)

canvas.pack(fill='both', expand=1)

tkimage = ImageTk.PhotoImage(bgimage)

canvas.create_image(0, 0, image=tkimage, anchor='nw', tag='images')

# bind the mouse events for selecting region

win.bind('', on_mouse_down)

win.bind('', on_mouse_move)

win.bind('', lambda e: win.destroy())

# use Esc key to abort the capture

win.bind('', lambda e: win.destroy())

# make the capture window modal

win.focus_force()

win.grab_set()

win.wait_window(win)

root.deiconify() # restore root window

# show the capture image

if roi_image:

show_image(roi_image)

tk.Button(root, text='select area', width=30, command=area_sel).pack()

root.mainloop()

During selecting region:

Show the capture image after selecting region:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值