Introduction to the ImageGrab module of Python image processing library PIL

Source: Internet
Author: User
Tags image processing library

Introduction to the ImageGrab module of Python image processing library PIL

The module is used to copy the content of the current screen or the content on the clipboard to the PIL image memory.

The current version only supports windows.

1. Functions of the ImageGrab Module

1. Grab

Definition: ImageGrab. grab ()? Image

ImageGrab. grab (bbox )? Image

Meaning: (New in 1.1.3) capture the snapshot of the current screen and return an image in RGB mode. The parameter boundary box is used to restrict only copying part of the current screen.

Example:

>>> from PIL importImage, ImageGrab>>> im =ImageGrab.grab()>>> im.size(1366, 768)>>> im.mode'RGB'>>> im.show()>>> im0 =ImageGrab.grab((300, 100, 1400, 600))>>> im0.show()>>> im0.size(1100, 500)>>> im0.mode'RGB'

The image im is a copy of the entire screen. Its size is 1366x768, which is the current resolution size of my monitor. The image im0 copies the Screen Content of 300,100,140x600 in the area (0.

The image im is as follows:

The image im0 is as follows:

2. Grabclipboard

Definition: ImageGrab. grabclipboard ()? Image or list of strings or None

Meaning: (New in 1.1.4) capture the snapshot of the current clipboard and return a list of images or file names in RGB mode. If the clipboard does not contain image data, this function returns NULL.

You can use the isinstance () function to check whether a valid image object or other data is returned by the function.

Example:

from PIL import Image, ImageGrabim = ImageGrab.grabclipboard()if isinstance(im, Image.Image):    print "Image: size : %s, mode: %s" % (im.size, im.mode)    im.save("D:\\Document\\mdoc\\python\\pic\\12\\grab_grabclipboard.jpg")elif im:    for filename in im:        try:            print "filename: %s" % filename            im = Image.open(filename)                    except IOError:            pass #ignore this file        else:            print "ImageList: size : %s, mode: %s" % (im.size, im.mode)else:    print "clipboard is empty."

Through the experiment, we found that opening the image in the drawing tool, selecting a part and cutting the paste will return an image in RGB mode. If the image file is clipped under the folder, "clipboard is empty." is returned, that is, the obtained clipboard content is empty or non-image content.

The results of this test are as follows:

>>>================================ RESTART ================================>>> Image: size : (566, 335),mode: RGB>>> im.size(566, 335)>>> im.mode'RGB'>>> im.show()

The image im is as follows:

 

Ii. isinstance functions in Python

Isinstance is a built-in function in Python.

Syntax:

Isinstance (object, classinfo)

If the parameter object is an instance of classinfo, or the object is an instance of a subclass of the classinfo class, True is returned. If an object is not of the specified type, the returned result is always False.

If classinfo does not represent a class (type object), it is either a class tuples, or recursively contains such (consisting of data types) tuples. other sequence types are not allowed.

If classinfo is not a data type or a tuples composed of data types, a TypeError exception is thrown.

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

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.