Python to write Screen capture program method _python

Source: Internet
Author: User

The program being written uses a lot of windows under the operation to look up a lot of data. See the operation of the Clipboard, think of the previous want to do a small program, then did not do, now just right to finish.

Function: Press the Printscreen key to carry on the screenshot, the data saves in the shearing board inside, very inconvenient. For example, when the game cut a moment of the picture, but you can not quit the game to save pictures, inconvenient multiple screenshots. And I do not like to install a variety of software, so ready to write this tool.

Train of thought: One is a custom shortcut key, screenshot, save. Taking into account the possibility of various conflicts, cancellation. Then still use to press Printscreen to screenshot, and then read picture data from the Clipboard, save. The idea is, first listen to the keyboard keys, when the Printscreen button, read the Clipboard content, and finally save the picture to the specified location.

1 monitor keyboard keys: Find data from the Internet, install Pywin32,pyhook. Link: http://sourceforge.net/projects/pyhook/,http://sourceforge.net/projects/pywin32/. Tutorial: Http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial.

2 Read the Clipboard content, but also need pywin32. Document in: [Pythonpath]\lib\site-packages\pywin32.chm, Online: http://timgolden.me.uk/pywin32-docs/index.html

Several formats given in the document are not data saved by the picture, Google search "Standard Clipboard Formats", Link: http://msdn.microsoft.com/en-us/library/windows/ Desktop/ff729168%28v=vs.85%29.aspx, all the format, is mainly 1-17.

Fortunately, there is a function in the document: Getpriorityclipboardformat, you can return the format of the Clipboard from an iterator. So the manual input was found, and y is sometimes 6,win32con. Cf_tiff, it's going to be 2. Of course, it's just a good idea to return the data here, and you don't need to know what it is.

Mainly used to:

Openclipboard,closeclipboard,getpriorityclipboardformat,getclipboarddata, these function documents are introduced, mainly speaking of CloseClipboard, the official document says , do not call CloseClipboard after you place the object in the Clipboard.

3 Save Pictures:

Found here can directly use the PIL module, directly solve the problem, above too tortuous t_t.

You can grab the screen directly using Imagegrab.grab () or use Imagegrab.grabclipboard () to get the image from the Clipboard.

Finally becomes, listens the key, presses the Printscreen, uses the PiL screenshot to save. T_t feels better than setting shortcuts, which should take up less memory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.