PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.
Image.draft() Configures the image file loader so it returns a version of the image that as closely as possible matches the given mode and size. For example, you can use this method to convert a color JPEG to grayscale while loading it, or to extract a 128×192 version from a PCD file.
Syntax: Image.draft(mode, size)
Parameters:
mode – The requested mode.
size – The requested size.
Returns: An Image object.
Return type: Image
Image Used:
Python3
# importing image object from PIL from PIL import Image # creating an image object im = Image. open (r"C:\Users\System - Pc\Desktop\rose.jpg") # print the original image object print (im) # using draft function # convert mode and size as well im1 = im.draft("L", (im.width / / 2 , im.height / / 2 )) im2 = im1.decoderconfig, im1.mode, im.size, im1.tile print (im1) print (im2) # show the converted image im1.show() |
Output1:
PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=217x232 at 0x27A3D65FD68 PIL.JpegImagePlugin.JpegImageFile image mode=L size=109x116 at 0x27A3D65FD68 ((2, 0), 'L', (109, 116), [('jpeg', (0, 0, 109, 116), 0, ('L', ''))])
Output2:
Another Example: Here we use another image.
Image Used:
Python3
# importing image object from PIL from PIL import Image # creating an image object im = Image. open (r"C:\Users\System - Pc\Desktop\tree.jpg") # print the original image object print (im) # using draft function # convert mode and size as well im1 = im.draft("L", (im.width / / 2 , im.height / / 2 )) im2 = im1.decoderconfig, im1.mode, im.size, im1.tile print (im1) print (im2) # show the converted image im1.show() |
Output1:
PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=259x194 at 0x28A1C2C1CC0 PIL.JpegImagePlugin.JpegImageFile image mode=L size=130x97 at 0x28A1C2C1CC0 ((2, 0), 'L', (130, 97), [('jpeg', (0, 0, 130, 97), 0, ('L', ''))])
Output2: