Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to process Image File by Pillow Library in Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report