Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

One, foreword

As of today, I’m not aware of any situations where you need to batch convert images to black and white. So the topic of this article is simple manipulation of pictures. In Python3, there is a third-party Pillow library that can be used to manipulate images. It is very simple to use, but it is perfect to see how this works.

Second, the use of Pillow

1, install,

The Pillow setup is simple, we just need to execute the following statement:

pip install pillow
Copy the code

After the installation is complete, you can import it using the following statement:

import PIL
Copy the code

Import PIL to fit python2 PIL library.

2. Read and display

The Pillow reads and displays images is very simple. The code is as follows:

# Import PIL submodule Image
from PIL import Image
# fetch image
img = Image.open("test.jpg")
# display images
img.show()
Copy the code

Here Image is a child module of Pillow, where the image.open function can be used to read the Image. This function returns a special Image object, and we can use some methods of this object. The img.show method is used to display images.

2, convert to black and white pictures

We can call the convert function of the image to convert the image mode. Here there are RGB, ARGB, GRAY, etc. GRAY refers to GRAY image, which is often called black and white image, the code is as follows:

from PIL import Image
img = Image.open("test.jpg")
# Convert to black and white images
gray = img.convert("L")
# Save black and white images
gray.save("11.jpg")
Copy the code

Here’s what the conversion looks like:

So here we giveconvertPassed a parameter “L”, is the meaning of transformation called gray map. And we also callsaveMethod to save the image.

Three, batch conversion of black and white pictures

Here, we combine the OS module to convert the pictures under the specified directory into black and white pictures. To generate the image, we need to create a folder:

import os
if not os.path.exists("test_gray"):
    os.mkdir("test_gray")
Copy the code

By executing the above code, we can create a test_gray folder in the current directory. Here we can get the path of all the images:

dirname = "test"
Get the list of paths to the image
imgs = [os.path.join(dirname, i) for i in os.listdir(dirname)]
Copy the code

The code here can be equivalent to:

imgs = []
dirname = "test"
for img in os.listdir(dirname):
    path = os.path.join(dirname, img)
    imgs.append(path)
Copy the code

I won’t explain it here. Then it is converted to black and white image operation, the complete code is as follows:

import os
from PIL import Image
if not os.path.exists("test_gray"):
    os.mkdir("test_gray")
dirname = "test"
imgs = [os.path.join(dirname, i) for i in os.listdir(dirname)]
for idx, img in enumerate(imgs):
    if img.endswith("jpg") or img.endswith("png") or img.endswith("jpeg"):
        img = Image.open(img)
        gray = img.convert("L")
        gray.save("test_gray/" + str(idx) + ".jpg")
Copy the code

Run the above code can achieve a picture into a black and white picture operation.