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 convert color pictures to black-and-white pictures in batch by Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how Python batch converts color pictures into black-and-white pictures. The article is rich in content and analyzes and narrates for you from a professional point of view. I hope you can get something after reading this article.

Recently encountered a new situation, the need to convert color pictures into black-and-white pictures, if you manually convert a picture, it is really Taiyuan primitive people.

With Python, it only takes a few lines of code to convert thousands of pictures in seconds. Moreover, this function has a wide range of uses, which can not only make the picture profound, but also simplify the mathematical calculation of some complex models, as shown in the analysis of the third part.

1. Prepare for

Before you begin, make sure that Python and pip are successfully installed on your computer. Oh, if not, please visit this article: hyperdetailed Python installation Guide for installation.

Open Cmd (start-run-CMD) in Windows environment, and open Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.

Of course, I recommend that you use the VSCode editor to Copy the code of this article and install the dependency module in the terminal at the bottom of the editor. What a comfortable thing: the best partner of Python programming-VSCode detailed guide.

Enter the following command at the terminal to install the dependent module we need:

Pip install pillow-I https://pypi.tuna.tsinghua.edu.cn/simple some-package

If you see Successfully installed xxx, the installation is successful.

two。 Write code

First of all, write the conversion function of our color map to monochromatic map, and receive two parameters, one is the path of the input file, the other is the path of the output file:

Familiar comments are longer than code series

Then put the images you need to convert into a folder. Here, I name the folder imgs, and then specify an output folder output, as follows:

What is more difficult to understand here is image_filenames, which uses list derivation, that is, traversing all the files in dataset_dir:

For file_dir in os.listdir (dataset_dir)

And link it to the directory we specified in the form of a path:

Os.path.join (output_dir, file_dir)

Finally, an array is generated, with the first element being the input path and the second element being the output path:

Image_filenames = [(os.path.join (dataset_dir, file_dir), os.path.join (output_dir, file_dir)) for file_dir in os.listdir (dataset_dir)]

3. Effect and use

The black-and-white effect goes like this:

Of course, there are many uses, such as:

1. When classifying images in deep learning, if you do not need to consider the color dimension, you can first convert the picture into a black-and-white image to simplify the computational complexity.

two。 Black-and-white pictures sometimes have more impact than color pictures and can highlight the theme.

3. Black-and-white pictures sometimes have the effect of beauty, because the focus of attention is on the shape of the face. if the person's face is particularly good-looking, but the skin color is poor, the black-and-white photo is very effective.

The above is the Python that the editor shares with you how to convert color pictures into black and white pictures in batches. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

*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