Cut images in batches without overlapping as much as possible and save [Python]

Sometimes, we need to split a large image into multiple small images. There are tons of methods available online, but most of these methods only allow you to ensure that the small pieces do not overlap, or have no control over the final number of cuts. This article describes the functions sklearn.feature_extraction.imagein the library extract_patches_2d. This function can split an image into multiple overlapping or non-overlapping small images for further processing or analysis. When you specify the number of cuts to be made, the program will cut your image as non-overlappingly as possible. As shown below. 


 How is this program implemented? This article will first introduce the use of this function, and then I will give a sample program to directly help you batch cut pictures.

Table of contents

 〇, preparation work, PIL library installation

1. Function introduction

2. Convert images in batches


 〇, 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

Meanwhile, you can use the pip command to install scikit-learn, ie sklearn. Enter the following command on the command line to install:

pip install -U scikit-learn

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

and sklearn:

conda install scikit-learn

1. Function introduction

To use extract_patches_2dthe function, you need to specify the input image, the patch size and the maximum number of patches. The tile size refers to the size of each tile to be extracted, usually expressed in pixels. Maximum number of tiles refers to the maximum number of tiles to be extracted from the image. If no maximum number of tiles is specified, the function will extract as many non-overlapping tiles as possible.

For example, if you have a 500x500 pixel image and want to split it into overlapping patches of size 32x32 pixels, you can use the function as follows extract_patches_2d:

# 读取图像文件并将其转换为NumPy数组
image = plt.imread('image.jpg')

# 定义patch大小
patch_size = (32, 32)

# 定义要提取的最大patch数量
max_patches = None

# 从图像中提取patch
patches = extract_patches_2d(image, patch_size, max_patches=max_patches)

In the above code, we first use plt.imread()the function to load the image file as a NumPy array. Then, we defined the patch size to be 32x32 pixels and set the maximum number of patches to None to extract as many non-overlapping patches as possible. Finally, we use extract_patches_2da function to extract the patch from the image and save the result in patchesa variable.

2. Convert images in batches

In this case, I will introduce how to batch cut a large number of pictures in a folder.

import os
from PIL import Image
from sklearn.feature_extraction.image import extract_patches_2d
from numpy to np
# 定义输入和输出文件夹路径
input_dir = '/path/to/input/folder/'
output_dir = '/path/to/output/folder/'

# 定义patch大小
patch_size = (32, 32)

# 定义要提取的最大patch数量
max_patches = None

# 遍历输入文件夹中的所有图像文件
for filename in os.listdir(input_dir):
    if filename.endswith('.jpg') or filename.endswith('.png'):
        # 加载图像作为NumPy数组
        image = np.array(Image.open(os.path.join(input_dir, filename)))

        # 从图像中提取patch
        patches = extract_patches_2d(image, patch_size, max_patches=max_patches)

        # 将patch保存为图像文件
        for i, patch in enumerate(patches):
            # 将patch转换为PIL图像对象
            patch_image = Image.fromarray(patch)

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

            # 保存patch图像文件
            patch_filename = os.path.splitext(filename)[0] + '_patch{}.jpg'.format(i)
            patch_image.save(os.path.join(output_dir, patch_filename))

In this example, we first define the paths to the input and output folders, as well as the patch size and maximum number of patches to extract. We then ositerate over all image files in the input folder using the library and PILload them as NumPy arrays using the library. Next, we use extract_patches_2dthe function to extract the patches from the image and use PILthe library to save each patch as a separate image file. Finally, we save the output file to the output folder.

It's worth noting that I'm assuming here that the input folder contains only image files, and that all image files have the .jpgor .pngfile extension. If your folder contains other types of files or file extensions, please change the code accordingly. Also, this sample code saves the output file in JPEG format. patch_image.save()If you need to use another format, please change the file extension in the call accordingly .

It is not easy to create, if it helps you, please like and follow, thank you~

Guess you like

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