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:


[Python]
#! /Usr/bin/env python
# Coding = gb2312
 
# This module mainly provides the screenshot capture function and supports the following three screenshot capture methods:
#1. Capture full screen. Press CTRL + F1.
#2. capture the current window. Press CTRL + F2.
#3. Capture the selected area and press CTRL + F3.
# After capturing the file, the SAVE dialog box is displayed automatically. Select the path to save the file.
#*************************************** ****
# Update records
#0.1 2012-03-10 create by dyx1024
#*************************************** *****
 
Import pyhk
Import wx
Import OS
Import sys
From PIL import ImageGrab
Import ctypes
Import win32gui
Import ctypes. wintypes
 
 
Def capture_fullscreen ():
'''''
Function: capture images in full screen
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''
# Capturing images
Pic = ImageGrab. grab ()

# Saving images
Save_pic (pic)

Def capture_current_windows ():
'''''
Function: captures the current window.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''
# Window structure
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 ()

# Retrieving the current window handle
HWND = win32gui. GetForegroundWindow ()

# Retrieve the coordinates of the current window
Ctypes. windll. user32.GetWindowRect (HWND, ctypes. byref (rect ))
 
# Adjust coordinates
Rangle = (rect. left + 2, rect. top + 2, rect. right-2, rect. bottom-2)

# Capturing images
Pic = ImageGrab. grab (rangle)

# Save
Save_pic (pic)

Def capture_choose_windows ():
'''''
Function: capture the selected area. If you do not have this, use the QQ image capture Function.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''
Try:
# Load the dll used for QQ screenshot
Dll_handle = ctypes. 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.
OS. system ("Rundll32.exe CameraDll. dll, CameraSubArea ")
Failed t Exception:
Return
Else:
Try:
# If the dll is loaded successfully, the image capture function is called. Note: The number of parameters included in the function is not clearly analyzed.
# And type. Therefore, after the statement is executed, it indicates that the parameter is missing 4 bytes, but the screenshot function is not affected.
# Ignore exceptions
Dll_handle.CameraSubArea (0)
Failed t Exception:
Return
 
Def save_pic (pic, filename = 'unordered image .png '):
'''''
Function: saves an image by using a file pair box.
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. register the shortcut key.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''

# Create a hotkey handle
Hot_handle = pyhk. pyhk ()

# Register the capture full screen shortcut key CTRL + F1
Hot_handle.addHotkey (['ctrl ', 'f1'], capture_fullscreen)

# Register and capture the current window shortcut key CTRL + F2
Hot_handle.addHotkey (['ctrl ', 'F2'], capture_current_windows)

# Register and capture the selected region shortcut key CTRL + F3
Hot_handle.addHotkey (['ctrl ', 'f3'], capture_choose_windows)

# Start running
Hot_handle.start ()

If _ name _ = "_ main __":
Main ()
#! /Usr/bin/env python
# Coding = gb2312

# This module mainly provides the screenshot capture function and supports the following three screenshot capture methods:
#1. Capture full screen. Press CTRL + F1.
#2. capture the current window. Press CTRL + F2.
#3. Capture the selected area and press CTRL + F3.
# After capturing the file, the SAVE dialog box is displayed automatically. Select the path to save the file.
#*************************************** ****
# Update records
#0.1 2012-03-10 create by dyx1024
#*************************************** *****

Import pyhk
Import wx
Import OS
Import sys
From PIL import ImageGrab
Import ctypes
Import win32gui
Import ctypes. wintypes


Def capture_fullscreen ():
'''
Function: capture images in full screen
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''
# Capturing images
Pic = ImageGrab. grab ()

# Saving images
Save_pic (pic)

Def capture_current_windows ():
'''
Function: captures the current window.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''
# Window structure
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 ()

# Retrieving the current window handle
HWND = win32gui. GetForegroundWindow ()

# Retrieve the coordinates of the current window
Ctypes. windll. user32.GetWindowRect (HWND, ctypes. byref (rect ))

# Adjust coordinates
Rangle = (rect. left + 2, rect. top + 2, rect. right-2, rect. bottom-2)

# Capturing images
Pic = ImageGrab. grab (rangle)

# Save
Save_pic (pic)

Def capture_choose_windows ():
'''
Function: capture the selected area. If you do not have this, use the QQ image capture Function.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''
Try:
# Load the dll used for QQ screenshot
Dll_handle = ctypes. 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.
OS. system ("Rundll32.exe CameraDll. dll, CameraSubArea ")
Failed t Exception:
Return
Else:
Try:
# If the dll is loaded successfully, the image capture function is called. Note: The number of parameters included in the function is not clearly analyzed.
# And type. Therefore, after the statement is executed, it indicates that the parameter is missing 4 bytes, but the screenshot function is not affected.
# Directly ignore some exceptions: www.2cto.com
Dll_handle.CameraSubArea (0)
Failed t Exception:
Return

Def save_pic (pic, filename = 'unordered image .png '):
'''
Function: saves an image by using a file pair box.
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. register the shortcut key.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-03-10
'''

# Create a hotkey handle
Hot_handle = pyhk. pyhk ()
 
# Register the capture full screen shortcut key CTRL + F1
Hot_handle.addHotkey (['ctrl ', 'f1'], capture_fullscreen)

# Register and capture the current window shortcut key CTRL + F2
Hot_handle.addHotkey (['ctrl ', 'F2'], capture_current_windows)

# Register and capture the selected region shortcut key CTRL + F3
Hot_handle.addHotkey (['ctrl ', 'f3'], capture_choose_windows)
 
# Start running
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 exe format (currently only used in windows and Linux)


From Socrates Column
 

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.