preamble
Bi has a part to use a function similar to QQ screenshot, here to record the production process. Because of the later to add artificial intelligence features, so use python to write desktop applications.
I. Needs analysis
The process can be simply divided into the following three steps:
Click the button or hit the shortcut key to enter the screenshot mode; press/release the left mouse button to select the rectangular area in the screenshot mode; save the screenshot of the rectangular area to the specified directory or clipboard.
Now I will search, write, and validate one step up from step three.
II. Screenshots
Through the "python" and "screenshot" two keyword search is not difficult to find python has a variety of functions to meet our needs, here we choose the PIL library grab () function. time function is used to test the speed of screenshot.
from PIL import ImageGrab import time # Capture the image def catch_area(left, top, right, bottom): return ((left, top, right, bottom)) # Test Functions def test(): first = () catch_area(101,101,200,300).save('D:/') print(() - first) test()
III. Rectangular selection
The screenshot function takes four parameters, the coordinates of the top, bottom, left and right of the rectangular area. In order to get these four parameters we need to listen for mouse clicks. I would have liked to search for a global listener api, here the choice is pyhook. but testing found that it does not support python2.7, and python3.7 installation process is extremely complex, so give up this method.
Then I thought I could use the GUI to generate a full-screen button, and then listen to the click event of this button. The GUI here is wxPython, version 3.7 installed by pip.
import wx from PIL import ImageGrab class ScreenShot(): left,right,top,bottom = 0,0,0,0 img = None def __init__(self, parent): .__init__(self, parent, style = # Full screen display ) # Setting the background color ((255,255,255)) # Set transparency (30) # Registration events (wx.EVT_LEFT_DOWN, , self) (wx.EVT_LEFT_UP, , self) # Show button (True) def OnDown(self, event): pos = () = = def OnUp(self, event): pos = () = = print(, , , ) = self.catch_area( , , , ) # Close the button when the screenshot is done (False) def catch_area(self, left, top, right, bottom): return ((left, top, right, bottom)) # Test cases # app = (False) # frame = ScreenShot(None) # ()
Here is just the intercepted image in the img variable, as for the subsequent operation, please implement your own.
IV. Button Settings
Finally, you need to construct a button and set a shortcut/hotkey for it. the GUI also uses the same wxPython as before. the page is just random; set a shortcut that is bound to a keyboard event for a certain control, which is needed to gather the focus to a certain control; and the hotkey is available globally, and hotkeys can be set up to bind the event to a menu option, or can be registered. Here directly register hotkeys.
import wx import ScreenShot as SS class MainWindow(): def __init__(self, parent): # Windows properties .__init__(self, parent, size = (1000, 200), # Windows size style = wx.CLIP_CHILDREN, # windows style, this style removes the native title bar #pos = (200, 200) # window position, centered below ) # Create a status bar at the bottom of the window () # Layout = () = () = () = [u"Starting screenshot.", u"Exit."] = [] for i in range(0, 2): # New keys ((self, -1, [i])) ([i], 1, ) # New read-only text box (reserved) = (self, style = wx.TE_READONLY) (, 1, ) (, 1, ) (, 4, ) # Activate the layout () (True) # (self) # Adapt to internal size # Event registration (wx.EVT_BUTTON, , [0]) (wx.EVT_BUTTON, , [1]) # Hotkey registration = () # create id (, wx.MOD_CONTROL, ord('Q')) # Register hotkeys (ctrl+Q response) (wx.EVT_HOTKEY, , id=) # Bind hotkey events # Window centering () # Show Windows (True) # Start taking screenshots def OnStart(self, event): (self) # Close the entire application def OnExit(self, event): () # Hotkey events def OnKeyBoard(self, event): (self) app = (False) frame = MainWindow(None) ()
summarize
The last two code blocks are the full application. This is just a very rough application, many details are not optimized, such as minimize to tray, auto-mark region when selecting region. If you have the ability to implement your own. This article on Python to achieve a simple QQ screenshot of the article is introduced to this, more related python QQ screenshot content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!