Python PIL library image graying processing

2020 April 4th, 2015 was a special day. We saw many grayed out pictures in our circle of friends. Today we will talk about the grayscale processing of pictures.

The basic concept of PIL:

The basic concepts involved in PIL are as follows: channel (bands), mode (mode), size (size), coordinate system (coordinate system), palette (palette), information (info) and filters (filters) ).

PIL (Python Image Library) is a third-party image processing library for python, but due to its powerful functions and a large number of users, it has almost been regarded as the official python image processing library. Its official homepage is: PIL. PIL has a long history, originally it only supports the python2.x version. Later, a library ported to python3 pillow appeared. Pillow is known as friendly fork for PIL. Its function is similar to PIL, but it supports python3.

There are many libraries that Python can process images: Matplotlib, OpenCV, TensorFlow, PIL, etc. Our protagonist today is: PIL (Python Imaging Library). Our environment is Python 3.8.2

installation

pip install Pillow

coding

import os
try:from PIL import Image
except ImportError as e:print(e)print('pip install Pillow')
 os._exit(0)
img_url ='/Users/rainbird/Desktop/a.jpg'
img_out = img_url.replace('.jpg','_gray.jpg')if not os.path.exists(img_url):print(f'file not found: {img_url}')
 os._exit(0)print(f'''
image in:{img_url}
image out:{img_out}'''.strip())
img_org = Image.open(img_url)
img_gray= img_org.convert('L') 
img_gray.save(img_out)print('done')

The code is not long, mainly:

Import the library and judge the exception;

Specify the file to be processed, and modify it when the individual runs;

After conversion, a file with the same name _gray will be generated

Currently only processed .jpg

run

image in:/Users/rainbird/Desktop/a.jpg
image out:/Users/rainbird/Desktop/a_gray.jpg
done

effect

to sum up

So far, this article on the image graying processing of the Python PIL library is introduced. For more related Python image graying content, please search the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. Cn!

Recommended Posts

Python PIL library image graying processing
Python on image processing PIL
python PIL open\display\save image
python_ file processing
Python processing json summary
Python datetime processing time summary
Python implements panoramic image stitching
python opencv for image stitching
Python implements image stitching function