Python: The PIL library Image class reads the image and returns the channel mismatch problem

Project scenario:

In the direction of computer vision, the author needs to read the picture, and then "throw" it to the written neural network for training, but in the process of throwing, the number of channels often does not match, which leads to problems in parameter input


Problem Description

Use the Image class in the PIL library to read RGB images, but only one channel is returned

Cause Analysis:

In the code, the image reading of the data set uses the Image.open method in the PIL library, and the image reading mode of Image.open is "L" by default, that is, a grayscale image, so the returned image has only one channel .

solution:

If you need to read a color image, you can change the parameter in the Image.open method to "RGB" to return an image with three channels. For example, the following line can be changed:

# 替换前
image = Image.open(image_path)

# 替换后
image = Image.open(image_path).convert('RGB')

Guess you like

Origin blog.csdn.net/qjyws/article/details/129350737