Get the App
SLTechnology News&Howtos  ›  Development  › 

How to process Image File by Pillow Library in Python

Shulou Source: shulou.com Published: 2022-06-01 08:37:00 09月16日 Update

Xiaobian to share with you how Python Pillow library image file processing, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Open PyCharm and create a new.py file.

2. Configuration environment

In this article, Python 3.6 version development click ok 2, library installation use

Enter pilot in the search field, select the first pilot, and install.

Installation complete!

In this figure we see that the pilot library has been configured successfully.

3. Overview of PIL Library

The PIL library can fulfill the functional requirements of image archiving and image processing:

(1) Image archiving: batch processing, image preview generation, image format conversion, etc.;

(2) Image processing: image basic processing, pixel processing, color processing, etc.

4. Code segment

The pictures used this time are:

The absolute path is D:\python jobs\dog.jpg

#Image processing: from PIL import Imagefrom PIL import ImageFilterfrom PIL import ImageEnhanceimg = Image.open ("D:\python\dog.jpg")print(img.format) #Output basic picture information print(img.mode)print(img.size)img_resize = img.resize((256,256)) #Adjust size img_resize.save("dogresize.jpg")img_rotate = img.rotate(45) #rotate img_rotate.save("dorotate.jpg")om=img.convert('L') #greyscale om.save ('doggray.jpg ')om = img.filter(ImageFilter.CONTOUR) #Image outline om.save ('dogcontour.jpg ')om = ImageEnhance.Contrast(img).enhance(20) #Contrast is 10 times the original om.save ('dogencontrast.jpg ')#Change image format: from PIL import Imageos filelist =["dog.jpg", "dogcontour.jpg", "dogencontrast.jpg", "doggray.jpg", "dogresize.jpg", "dogrotate.jpg", ]for infile in filelist: outfile = os.path.splitext(infile)[0] + ".png" if infile != outfile: try: Image.open(infile).save(outfile) except IOError: print ("cannot convert", infile)

Right mouse click Run

operation results

The above is all the content of this article "How to process image files in Pillow library in Python", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Image processing picture file article content image processing format run configuration success not so much code information pixels function most contrast size aspect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS vpn Xiaomi Docker