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 use Pillow to process pictures in python

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to use Pillow to deal with pictures in python, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Create a new python file named py3_pil.py, and write the operation code in this file:

# use Pillow for image processing # need pip install Pillow# import pillow module Imagefrom PIL import Image,ImageFilter# to open picture object image1 = Image.open ('dog.png') # image1.show () # automatically call the system picture viewer to open the picture after running

# change the picture to jpg format and save it # notice here # PNG picture is four channels: RGBA means red, green, blue, # Alpha color space, Alpha refers to transparency # JPG means three channels: RGB means red, green, blue # so you need to convert to three channels in saving image1 = image1.convert ('RGB') image1.save (' dog.jpg')

# Let's take a look at an example of batch modification of the image format: all the contents of the directory where the current file is located in the import os# loop for f in os.listdir ('.'): # find the image whose file ends with .jpg # convert it to png format # Save it to the pngs directory to if f.endswith ('.jpg'): I = Image.open (f) fn Ext = os.path.splitext (f) i.save ('pngs/ {} .png' .format (fn)) # the running effect is shown in screenshot 1 below

# modify the size of the picture and save # define a tuple size_200 = (200200) for f in os.listdir ('.'): # find the image ending with .jpg in the file # convert it to png format # Save it to the pngs directory to if f.endswith ('.jpg'): I = Image.open (f) fn,ext = os.path.splitext (f)

# use thumbnail () to resize the image pixel i.thumbnail (size_200) i.save ('pngs_200/ {} _ 200.png'.format (fn)) # for more information, please see screenshot 2

# use rotate to rotate the image image1 = Image.open ('dog.jpg') image1.rotate (90). Save (' dog_mod.jpg') # for more information, please see screenshot 3

# use convert to convert a picture to gray image1 = Image.open ('dog.jpg') image1.convert (mode='L'). Save (' dog_conv.jpg') # for more information, please see screenshot 4.

# use ImageFilter blurred images image1 = Image.open ('dog.jpg') image1.filter (ImageFilter.GaussianBlur (15)). Save (' dog_blur.jpg') # the results can be seen in figure 5. This is the answer to how to use Pillow to deal with pictures in python. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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: 262

*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

Internet Technology

Wechat

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

12
Report