Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Pillow Library to process Image Files in Python

Shulou Source: shulou.com Published: 2022-06-02 07:45:22 09月20日 Update

This article is about how to use the Pillow library to deal with image files in Python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Pillow is a very useful library for dealing with images in python. This time I will mainly introduce the basic use of pillow and know what requirements it can solve.

Review Image Mode (mode)

The image coordinate system used by the pillow library

The coordinates of the image start from the upper left corner (0minute 0). The coordinate value represents the angle of the pixel, which is actually located at (0.5). The coordinates in python are usually transmitted in the form of 2 tuples (XMagi Y), and the rectangle is represented as 4 tuples (LilixGravity Tincy, ronomex journal). The X axis is from left to right, and the Y axis is from top to bottom. The order is from the upper left to the lower right. From the upper left corner, such as a 800X600 pixel image rectangle, it is actually locked in the upper left corner and extends to the lower right.

Graphics

Install the pillow method

Pip install pillow

Common methods of pillow

The most common operation is the zoom operation of the image, the code is as follows

From PIL import Image

# Open a jpg image file Note that it is the current path: im = Image.open ('boat_girl.jpg') # to get the image size: W, h = im.sizeprint (' the length and width of the picture is:% sx%s'% (w, h)) # zoomed to 50%:im.thumbnail (w _ sx%s' _ 2, h _ sx%s' _ 2) print ('the length and width of the picture scaled by 50% is:% sx%s'% (w _ print 2) ) # Save the scaled image in jpeg format: im.save ('boat_girl_small.jpg',' jpeg')

The image blur operation is

From PIL import Image, ImageFilter

# Open a jpg image file, note that it is the current path: im = Image.open ('boat_girl.jpg') # apply blur filter: im2 = im.filter (ImageFilter.BLUR) im2.save (' boat_girl_blur.jpg', 'jpeg') above is how to use the Pillow library in Python to deal with image files. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Images files pictures processing coordinates upper-left corner pixels actually methods more knowledge rectangles articles paths utility code graphics coordinate systems that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux MariaDB Microsoft MySQL