The ImageGrab module is used to copy the contents of the current screen or the clipboard to the PIL image memory.
The current version only supports windows.
I. Functions of the ImageGrab module
1、 Grab
Definition: ()⇒ image
(bbox) ⇒ image
Meaning: (New in 1.1.3) Grab a snapshot of the current screen and return an image with mode "RGB". The bounding box is used to limit the copy to only a part of the current screen.
Example:
>>> from PIL importImage, ImageGrab >>> im =() >>> (1366, 768) >>> 'RGB' >>> () >>> im0 =((300, 100, 1400, 600)) >>> () >>> (1100, 500) >>> 'RGB'
Image im is copying the content of the entire screen in size 1366x768, the current resolution size of my monitor. Image im0 copies the content of the screen in the region (300, 100, 1400, 600) in the size 1100x500.
The images im as follows:
The image im0 is as follows:
2、 Grabclipboard
define:()⇒ image or list of strings or None
Meaning: (New in 1.1.4) Grab a snapshot of the current clipboard and return a list of images or file names with mode "RGB". If the clipboard does not contain image data, this function returns null.
The user can use the function isinstance() to check whether the function returns a valid image object or other data.
Example:
from PIL import Image, ImageGrab im = () if isinstance(im, ): print "Image: size : %s, mode: %s" % (, ) ("D:\\Document\\mdoc\\python\\pic\\12\\grab_grabclipboard.jpg") elif im: for filename in im: try: print "filename: %s" % filename im = (filename) except IOError: pass #ignore this file else: print "ImageList: size : %s, mode: %s" % (, ) else: print "clipboard is empty."
Through experimentation, I found that if I open an image in Paint tool, select a part of it and then cut and paste it, it will return an image with the mode of "RGB". If you cut and paste the image file in the folder, it will return "clipboard is empty.", that is, here to get the clipboard content is empty or non-image content.
The results of this test are as follows:
>>>================================ RESTART ================================
>>>
Image: size : (566, 335),mode: RGB
>>>
(566, 335)
>>>
'RGB'
>>> ()
The images im as follows:
II. isinstance function in Python
isinstance is a built-in function in Python.
Grammar:
isinstance(object,classinfo)
Returns True if the argument object is an instance of classinfo, or if object is an instance of a subclass of classinfo. if object is not an object of the given type, the return result is always False.
If classinfo does not represent a class (type object), then it is either a tuple of classes, or recursively contains such a tuple (of data types). Other sequence types are not allowed.
If classinfo is not a datatype or a tuple consisting of datatypes, a TypeError exception is raised.
Example:
>>>isinstance(100, int) True >>>isinstance(10.5, int) False >>>isinstance(10.5, float) True >>>isinstance(10.5, int) False >>>isinstance(10.5, (int,float)) True
screenshot
sample code (computing)
from PIL import Image from PIL import ImageGrab size = (300,300,400,400) img = (size) ("") ()
to this article on the Python image processing library PIL ImageGrab module to introduce the details of the article is introduced to this, more related to the PIL ImageGrab module content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!