Use Python to batch convert color images to grayscale images

When it comes to image processing and computer vision, sometimes it is necessary to convert color pictures to grayscale pictures, and it is very complicated and unnecessary to use tools such as PS to convert one by one. The method introduced today uses the Pillow library. Use the Pillow library to open, load and convert color images, and store the images in another folder. The specific steps are as follows.


Table of contents

 〇, preparation work, PIL library installation

1. Convert a color image to a grayscale image

2. Batch convert color images to grayscale images


 〇, preparation work, PIL library installation

The Pillow library (which is a fork of the Python Imaging Library) can be installed in Python with the following command:

pip install Pillow

Alternatively, it can be installed via scientific computing distributions such as Anaconda or Miniconda. If you use Anaconda, you can use the following command to install the Pillow library:

conda install pillow

1. Convert a color image to a grayscale image

Currently, our program uses methods in the Pillow library convert()to convert color images to grayscale images. The method accepts a string parameter specifying the mode to convert the image to. For grayscale images, the mode is 'L'. Here is an example of our code:

from PIL import Image

# 打开彩色图片
color_image = Image.open("color_image.jpg")

# 将彩色图片转换为灰度图片
gray_image = color_image.convert('L')

# 保存灰度图片
gray_image.save("gray_image.jpg")

In this example, we open color_image.jpgthe color image named and convert()convert it to a grayscale image using the method Finally, we save()saved the grayscale image using method . If the image you want to convert is another type of image, just replace .jpg with the format you need, such as .png.

2. Batch convert color images to grayscale images

With the conversion of one picture, the conversion of multiple pictures becomes simple. For the next batch processing, we will take the png suffix image as an example.

import os
from PIL import Image

# 设置输入和输出文件夹的路径
input_dir = "/path/to/input/directory"  # 输入文件夹路径
output_dir = "/path/to/output/directory"  # 输出文件夹路径

# 如果输出文件夹不存在,则创建输出文件夹
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 循环遍历输入文件夹中的所有PNG文件
for filename in os.listdir(input_dir):
    if filename.endswith(".png"):
        # 打开彩色图像
        color_image = Image.open(os.path.join(input_dir, filename))

        # 将彩色图像转换为灰度图像
        gray_image = color_image.convert('L')

        # 将灰度图像保存到输出文件夹中
        gray_image.save(os.path.join(output_dir, filename))

In this program, we need to set the paths of the input and output folders to our own file paths. Next, the program loop will input all the PNG files in the folder and check to see if the file .pngends with . For each PNG file, it opens the color image, converts it to a grayscale image, and saves the grayscale image to the output folder with the same filename.

Notice

Be sure to replace /path/to/input/directoryand /path/to/output/directorywith your own paths.

Guess you like

Origin blog.csdn.net/weixin_49030835/article/details/129538975