Python: A Multi-Function graph capture tool development (with source code)

Source: Internet
Author: User

In recent years, we have seen such replies in various forums: no picture, no truth. When you frequently write weekly work reports or experiment results, you need to paste several images. Clipboard), paste the image in the clipboard to the canvas, and then save it as a hassle. If you are in a company and cannot use QQ, it is a waste of time to capture a picture in a region and edit it through a picture board.

To solve the above problem, I wrote a graph capture tool using Python today. Three requirements are available: capture the screen with a shortcut key, capture the current window, and select the selected area. After capturing, the SAVE dialog box is displayed, you no longer need to use a picture board for a transfer. Go to the topic:

I. Code:

#! /Usr/bin/ENV Python # Coding = gb2312 # This module mainly provides the screenshot capture function and supports the following three ways: #1. Capture full screen, shortcut Key Ctrl + F1 #2. capture the current window. Shortcut Key Ctrl + F2 #3. Capture the selected area. After the shortcut key Ctrl + F3 # is captured, the SAVE dialog box is automatically displayed, select the path to save it #*********************************** * ******* # update record #0.1 2012-03-10 create by dyx1024 #********************** * ******************** import pyhkimport wximport osimport sysfrom Pil import imagegrabimport ctypesimport win32guiimport ctypes. wintypes def capture_fullscreen (): ''' function: full screen capture input: None output: None Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: 2012-03-10 ''' # capture image = imagegrab. grab () # Save the image save_pic (PIC) def capture_current_windows (): ''' function: capture the current window input: None output: None Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: 2012-03-10 ''' # class rect (ctypes. structure): _ fields _ = [('left', ctypes. c_long), ('top', ctypes. c_long), ('right', ctypes. c_long), ('bottom ', ctypes. c_long)] def _ STR _ (Self): Return STR (self. left, self. top, self. right, self. bottom) rect = rect () # obtain the current window handle hwnd = win32gui. getforegroundwindow () # obtain the current window coordinate ctypes. windll. user32.getwindowrect (hwnd, ctypes. byref (rect) # adjust coordinate rangle = (rect. left + 2, rect. top + 2, rect. right-2, rect. bottom-2) # capture image PIC = imagegrab. grab (rangle) # Save save_pic (PIC) def capture_choose_windows (): ''' function: capture the selected region. You have not written this by yourself. Use the QQ screenshot function input: None output: none Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: 2012-03-10 ''' try: # load the DLL dll_handle = ctypes for QQ capture. cdll. loadlibrary ('cameradll. dll ') failed t exception: Try: # If the DLL fails to be loaded, use another method and run it directly. If the DLL still fails, exit the OS. system ("rundll32.exe cameradll. DLL, camerasubarea ") failed t exception: Return else: Try: # After the DLL is loaded successfully, the image capture function is called. Note: The number of parameters and types of the function are not clearly analyzed, therefore, after the statement is executed, it will report that the parameter is missing 4 bytes, but the screenshot capture function is not affected. The # directly ignores some exceptions dll_handle.camerasubarea (0) failed t exception: Return def save_pic (PIC, filename = 'unordered image .png '): ''' function: use the File pair box to save the image input: None output: None Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: 2012-03-10 ''' APP = wx. pysimpleapp () wildcard = "PNG (*. PNG) | *. PNG "dialog = wx. filedialog (none, "select a place", OS. getcwd (), filename, wildcard, wx. save) If dialog. showmodal () = wx. id_ OK: pic. save (dialog. getpath (). encode ('gb2312') else: Pass dialog. destroy () def main (): ''' function: main function, registered shortcut key input: None output: None Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: ''' # create a hotkey handle hot_handle = pyhk. pyhk () # register the capture full screen shortcut Ctrl + F1 hot_handle.addhotkey (['ctrl ', 'f1'], capture_fullscreen) # register and capture the current window shortcut Ctrl + F2 hot_handle.addhotkey (['ctrl ', 'F2'], capture_current_windows) # register and capture the selected area shortcut Ctrl + F3 hot_handle.addhotkey (['ctrl ', 'f3 '], capture_choose_windows) # start to run hot_handle.start () If _ name _ = "_ main _": Main ()

Ii. test:

After the program runs, you can view other things. If you want to capture a picture, press the corresponding shortcut key based on the image type you want to capture. In addition to the first one, all the other parts of the program are used in the capture area.

1. The save dialog box is displayed automatically after the capture.

 

2. Capture full screen (CTRL + F1)

2. capture the current window (CTRL + F2)

3. Capture the selected area (CTRL + F3)

Iii. improvements to be made:

Later, we will consider adding functions such as Tray display and user-defined shortcut keys, and finally release them in the EXE format. (currently, we only use them in windows. If you are free in Linux, we will try again)

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.