Python writes screenshot program methods

  • 2020-04-02 14:35:44
  • OfStack

The program being written USES a lot of Windows operations and looks up a lot of information. When I saw the operation of the clipboard, I remembered that I wanted to do a small program before, but I didn't do it then, and now I just finished it.

Function: when you press the printscreen key to take a screenshot, the data is saved in the clipboard, which is inconvenient. For example, when the game cut a moment of the picture, but you can't quit the game to save the picture, not convenient for many screenshots. And I don't like to install all kinds of software, so I'm going to write this tool.

Train of thought: one is the custom shortcut key, screenshot, save. Considering the possibility of various conflicts, cancel. Again, press printscreen to take a screenshot, then read the image data from the clipboard and save it. The idea is to listen for the keyboard keys, read the clipboard when the printscreen key is pressed, and finally save the image to the specified location.

1. Monitor keyboard keys: find information from the Internet, install pywin32, pyhook. Link: (link: http://sourceforge.net/projects/pyhook/), (link: http://sourceforge.net/projects/pywin32/). Tutorial: http://sourceforge.net/apps/mediawiki/pyhook/index.php? Title = PyHook_Tutorial.

2 read the clipboard contents, also need pywin32. Documents in: [Pythonpath] \ Lib \ site - packages \ pywin32. CHM, online: (link: http://timgolden.me.uk/pywin32-docs/index.html)

Several formats given in the document are not the data stored in the image. Google search & # 65279; The & # 65279; "Standard Clipboard Formats," link, link: (http://msdn.microsoft.com/en-us/library/windows/desktop/ff729168%28v=vs.85%29.aspx), all of the format, the main is 1-17.

Have a function in the document: GetPriorityClipboardFormat, can return to the clipboard format, from an iterator. So I typed in by hand and I found that y is sometimes 6, win32con.cf_tiff, and then 2. Of course, I'm just going to return the data, I don't need to know what it is.

Mainly used:

OpenClipboard, CloseClipboard GetPriorityClipboardFormat, GetClipboardData, it has introduced several functions document, mainly about CloseClipboard, official documents, don't call CloseClipboard after placing objects in the clipboard.

3. Save pictures:

Here I found that I could directly use PIL module to solve the problem, which is too tortuous T_T.

You can grab the screen directly with imagegrab.grab () or get an image from the clipboard using imagegrab.grabboard ().

 

Finally, it becomes, listen to the button, press printscreen and save with pil screenshot. T_T feels better than setting shortcuts, which should take up less memory.


Related articles: