图片拼接|横向拼接|竖向拼接|正方形拼接|其他模式拼接 python

  • 读取某文件夹下所有子文件夹的图片,进行拼接

文件夹

---文件夹1

|----图片1

|----图片2

---文件夹2

|----图片3

---文件夹3

|----图片4

|----图片5

python 复制代码
import os
import cv2
import numpy as np
import random
from math import ceil, sqrt


# 支持中文路径的图片读取函数
# def cv_imread(file_path):
#     cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
#     cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)  # 转换为 BGR 格式
#     return cv_img

def cv_imread(file_path):
    """支持中文路径的图片读取,修正反色问题"""
    cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
    return cv_img  # 不再进行颜色转换,直接返回 BGR 图像



def load_images_from_folder(folder):
    """加载指定文件夹及其子文件夹中的所有图片,支持中文路径"""
    images = []
    for root, _, files in os.walk(folder):
        for file in files:
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff')):
                img_path = os.path.join(root, file)
                img = cv_imread(img_path)  # 使用支持中文路径的读取方法
                if img is not None:
                    images.append(img)
    return images


def vertical_stack(images):
    """竖向拼接"""
    return np.vstack(images)


def horizontal_stack(images):
    """横向拼接"""
    return np.hstack(images)


def square_stack(images):
    """拼接成接近正方形的布局"""
    n = len(images)
    cols = ceil(sqrt(n))
    rows = ceil(n / cols)
    max_height = max(img.shape[0] for img in images)
    max_width = max(img.shape[1] for img in images)

    # 创建空白画布
    canvas = np.zeros((rows * max_height, cols * max_width, 3), dtype=np.uint8)

    for idx, img in enumerate(images):
        row = idx // cols
        col = idx % cols
        y_offset = row * max_height
        x_offset = col * max_width
        canvas[y_offset:y_offset + img.shape[0], x_offset:x_offset + img.shape[1]] = img
    return canvas


def chaotic_stack(images):
    """恶搞模式:随机旋转、缩放和排列图片"""
    canvas_size = (1000, 1000, 3)
    canvas = np.zeros(canvas_size, dtype=np.uint8)
    h, w = canvas.shape[:2]

    for img in images:
        # 随机缩放
        scale = random.uniform(0.5, 1.5)
        resized_img = cv2.resize(img, (0, 0), fx=scale, fy=scale)

        # 随机旋转
        angle = random.randint(0, 360)
        center = tuple(np.array(resized_img.shape[1::-1]) // 2)
        rot_mat = cv2.getRotationMatrix2D(center, angle, 1.0)
        rotated_img = cv2.warpAffine(resized_img, rot_mat, resized_img.shape[1::-1], flags=cv2.INTER_LINEAR,
                                     borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))

        # 随机放置
        x_offset = random.randint(0, w - rotated_img.shape[1])
        y_offset = random.randint(0, h - rotated_img.shape[0])

        try:
            canvas[y_offset:y_offset + rotated_img.shape[0], x_offset:x_offset + rotated_img.shape[1]] = rotated_img
        except ValueError:
            # 如果超出范围,跳过
            continue
    return canvas


def save_image(output_path, image):
    """保存图片,支持中文路径"""
    cv2.imencode('.jpg', image)[1].tofile(output_path)


def main():
    folder = input("请输入图片文件夹路径:")
    output_path = input("请输入拼接后图片保存路径(如 output.jpg):")
    print("选择拼接模式:")
    print("1: 竖向拼接")
    print("2: 横向拼接")
    print("3: 拼成接近正方形")
    print("4: 恶搞模式")
    mode = int(input("请输入模式编号(1/2/3/4):"))

    images = load_images_from_folder(folder)
    if not images:
        print("未找到图片,请检查文件夹路径!")
        return

    if mode == 1:
        result = vertical_stack(images)
    elif mode == 2:
        result = horizontal_stack(images)
    elif mode == 3:
        result = square_stack(images)
    elif mode == 4:
        result = chaotic_stack(images)
    else:
        print("无效的模式编号!")
        return

    save_image(output_path, result)
    print(f"拼接完成,图片已保存至:{output_path}")


if __name__ == "__main__":
    main()
相关推荐
MyhEhud19 分钟前
kotlin @JvmStatic注解的作用和使用场景
开发语言·python·kotlin
狐凄32 分钟前
Python实例题:pygame开发打飞机游戏
python·游戏·pygame
漫谈网络40 分钟前
Telnet 类图解析
python·自动化·netdevops·telnetlib·网络自动化运维
农夫山泉2号2 小时前
【python】—conda新建python3.11的环境报错
python·conda·python3.11
ZHOU_WUYI3 小时前
Flask Docker Demo 项目指南
python·docker·flask
J先生x4 小时前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
码上淘金7 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo7 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
2301_787552878 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
懵逼的小黑子8 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django