In this tutorial, you will resize an image using PIL in Python. PIL is the Python imaging library that provides the python interpreter with image editing function. The Image modile provides a class with the same name which used to represent a PIL image and the module als provides a number of factory functions, including functions to load images from files, and to create new images. The syntax is explained as follows.
Image.resize() – Returns a resized copy of any given image.
size: the requested size in pixels, as a 2 tuple: (width, height).
resample: an optional resampling filters. This can be one of PIL. image.NEAREST (use nearest neighour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic spline interpolation), or PIL.Image.LANCZOS (a high-quality downsampling filter). If omitted, or if the image has mode ‘1’ or ‘P’, it is set PIL.Image.NEAREST.
Return type: An Image object.
The image that I used:
The code is show as follow.
# Created by inpows # Visit inpows.com for more python tutorials # Improting Image class from PIL module from PIL import Image # Opens a image in RGB mode im = Image.open(r"D:\MyProject\Python\ImageDownloader\lena.bmp") # Size of the image in pixels (size of orginal image) width, height = im.size # Setting the points for cropped image left = 4 top = height / 5 right = 299 bottom = 3 * height / 5 # Cropped image of above dimension # (It will not change orginal image) im1 = im.crop((left, top, right, bottom)) newsize = (300, 300) im1 = im1.resize(newsize) # Shows the image in image viewer im1.show()
The result will be: