Use Python to stitch pictures in batches

foreword

Image stitching techniques are often used when multiple images need to be stitched into one larger image. This technique has a wide range of applications in many fields such as computer vision, image processing, satellite imagery, geographic information systems, and more. In practical applications, stitched images can be used to create panoramas, maps, posters, billboards, and more.

This article will use the following four pictures as examples to introduce the method of using the PIL library in Python for picture stitching. We will use the Image module from the PIL library to load, resize and merge multiple images.

Table of contents

foreword

〇, preparation work, PIL library installation

1. Simple program realization

2. More complex situations



〇, preparation work, PIL library installation

PIL (Python Imaging Library) is a powerful image processing library in Python, which can realize various operations such as opening, saving, resizing, cropping, and merging of images. The PIL library also provides a wealth of image processing functions, such as color adjustment, filter effects, text overlay, etc., making image processing easier and more efficient.

The method of installing the PIL library is as follows:

  1. install using pip

Enter the following command on the command line:

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. Simple program realization

import os
import glob
from PIL import Image

# 设置图像文件夹的路径
image_dir = 'images/'

# 获取文件夹中所有图像文件的列表
image_files = glob.glob(os.path.join(image_dir, '*.png'))

# 加载每个图像,并调整为200x200像素大小(如果需要)
images = []
for image_file in image_files:
    image = Image.open(image_file)
    image = image.resize((200, 200))
    images.append(image)

# 创建一个新的400x400像素大小的白色背景图像
new_image = Image.new('RGB', (400, 400), 'white')

# 将四个图像粘贴到新图像的正确位置
new_image.paste(images[0], (0, 0))
new_image.paste(images[1], (200, 0))
new_image.paste(images[2], (0, 200))
new_image.paste(images[3], (200, 200))

# 将最终图像保存到磁盘上
new_image.save(image_dir + 'output.png')

The effect is as follows

2. More complex situations

What should I do if I encounter more pictures? I slightly changed the program structure so that you can splice any number of graphic blocks.

11/04/2023 version,  debuged this part.

30/04/2023 version,  debuged this part again.

import os
import math
from PIL import Image


def merge_images(image_folder, output_file, n, m):
    # 获取所有图像文件的列表
    image_files = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if f.endswith('.png')]

    # 计算每个小图像的大小和大图像的大小
    image_count = len(image_files)
    if image_count == 0:
        print('No image files found in the directory:', image_folder)
        return

    # 计算小图像的大小以及大图像的大小
    img = Image.open(image_files[0])
    img_size0 = img.size[0]
    img_size1 = img.size[1]
    new_img_size0 = img_size0 * n
    new_img_size1 = img_size1 * m

    # 创建一个新的大图像
    new_img = Image.new('RGB', (new_img_size0, new_img_size1), 'white')

    # 将所有小图像粘贴到新图像的正确位置
    for i, f in enumerate(image_files):
        row = int(i / n)
        col = i % n
        img = Image.open(f)
        img = img.resize((img_size0, img_size1))
        new_img.paste(img, (col * img_size0, row * img_size1))

    # 保存大图像
    new_img.save(output_file)


# 用法示例
image_folder = 'C:/Users/someone/Desktop/img_denosing_test/paper_pics/set68/result'
output_file = 'C:/Users/someone/Desktop/img_denosing_test/paper_pics/set68/result/output.png'
n = 3  # 每行显示的图像数
m = 2  # 每列显示的图像数
merge_images(image_folder, output_file, n, m)

In this article, we demonstrated how to use the PIL library in Python for image stitching, allowing you to easily handle various image stitching tasks.

Guess you like

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