[Python] Convert color images to grayscale images (a 4-line script)

I read a lot of other people's scripts on the Internet; it is
not easy to use, and then I can get it in 4 lines;

1 Install the PIL library

pip install pillow

2 Create a folder

Create a folder containing color pictures and ready to be saved as grayscale images;
We created a folder named change_fig
we created a folder named change_fig;
fig1.jpg is our original color image;
fig2.jpg is the grayscale image we are about to generate;
trans. py is our script;

3 Script content

from PIL import Image
I = Image.open('./fig1.jpg')
L = I.convert('L')
L.save("./fig2.jpg")	# 灰度图fig2.jpg保存在当前目录
# L.show()

4 show

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/ao1886/article/details/110649649