图片拼接|横向拼接|竖向拼接|正方形拼接|其他模式拼接 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()
相关推荐
冷雨夜中漫步2 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴2 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再2 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手4 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934734 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
啊森要自信4 小时前
CANN ops-cv:面向计算机视觉的 AI 硬件端高效算子库核心架构与开发逻辑
人工智能·计算机视觉·架构·cann
helloworldandy4 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威6 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ6 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha6 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全