OpenCV、PIL 切割合并图片

38 篇文章 4 订阅
11 篇文章 3 订阅

import os
import cv2
import numpy as np
import math
from PIL import Image


def crop_one_picture(path,save_split_path,height_min,width_min):
    """
    切割图片
    :param path: 原图路径
    :param save_split_path: 切割小图保存路径
    :param height_min: 切割尺寸
    :param width_min: 切割尺寸
    :return:
    """
    for root, dirs, files in os.walk(path):
        for file in files:
            img = Image.open(os.path.join(root, file))
            # 高度
            sum_rows = img.size[0]
            # 宽度
            sum_cols = img.size[1]
            # 创建文件夹
            if not os.path.exists(save_split_path):
                os.makedirs(save_split_path)
            # 行列 切割
            for i in range(math.ceil(sum_cols/width_min)):
                for j in range(math.ceil(sum_rows/height_min)):
                    save_name = save_split_path + os.path.splitext(file)[0] + '_' + str(j) + '_' + \
                                str(i) + os.path.splitext(file)[1]
                    # 构建box 大小
                    box = (j*height_min, i*width_min, (j+1)*height_min, (i+1)*width_min)
                    # 切割
                    cropped = img.crop(box)
                    # 保存
                    cropped.save(save_name)
    print("裁剪完成!")

def merge_picture(path,save_split_path,save_merge_path,height_min,width_min, merge_type):
    """
    合并图片
    :param path: 原图路径
    :param save_split_path: 小图路径
    :param save_merge_path: 合图保存路径
    :param height_min: 小图尺寸
    :param width_min: 小图尺寸
    :param merge_type: 保存的类型
    :return:
    """
    # 创建文件夹
    if not os.path.exists(save_merge_path):
        os.makedirs(save_merge_path)
    for root, dirs, files in os.walk(path):
        for file in files:
            # 读取原图片,获取大小
            shape = cv2.imdecode(np.fromfile(os.path.join(root, file), dtype=np.uint8), cv2.IMREAD_COLOR).shape
            width = shape[1]
            height = shape[0]
            channels = shape[2]
            # 获取行和列
            rows = math.ceil(height/height_min)
            cols = math.ceil(width/width_min)
            # 创建底板(原图大小 + 黑边大小)
            dst = np.zeros((rows * height_min, cols * width_min, channels), np.uint8)
            for i in range(cols):
                for j in range(rows):
                    # 小图路径
                    split_name = save_split_path + os.path.splitext(file)[0] + '_' + str(j) + '_' + str(i) + os.path.splitext(file)[1]
                    img = cv2.imdecode(np.fromfile(split_name, dtype=np.uint8), cv2.IMREAD_COLOR)
                    # 合并
                    dst[i*height_min:(i+1)*height_min, j*width_min:(j+1)*width_min, :] = img
                    # 改变为原图的大小
                    dst_resize = dst[0:height, 0:width, :]
            cv2.imencode(ext=merge_type, img=dst_resize)[1].tofile(save_merge_path + file)
    print('合并完成!')


if __name__ == '__main__':
    # 原图路径
    path = './1/'
    # 切割小图保存路径
    save_split_path = './split_img/'
    # 合图保存路径
    save_merge_path = './merge_img/'
    # 切割大小
    height_min = 400
    width_min = 400
    # 合图保存的类型
    merge_type = '.tiff'
    # 切割
    crop_one_picture(path, save_split_path, height_min, width_min)
    # 合并
    merge_picture(path, save_split_path, save_merge_path, height_min, width_min, merge_type)

原图:
在这里插入图片描述

切割小图:
在这里插入图片描述
合并图:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python图像识别

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值