笔记一:拼接、补全图片

批量操作拼接 并保存拼接后图片(1024*1024)

# -*- coding: utf-8 -*-
"""
Created on Sat Dec 10 16:44:53 2022

@author: 38949
"""

import numpy as np
from PIL import Image
import os


# 判断是否需要进行图像填充
def judge(img, wi, he):
    width, height = img.size
    # 默认新图像尺寸初始化为原图像
    new_width, new_height = img.size
    if width % wi != 0:
        new_width = (width//wi + 1) * wi
    if height % he != 0:
        new_height = (height//he + 1) * he
    # 新建一张新尺寸的全黑图像
    new_image = Image.new('L', (new_width, new_height))
    # 将原图像粘贴在new_image上,默认为左上角坐标对应
    new_image.paste(img, box=None, mask=None)
    new_image.show()
    return new_image

# 按照指定尺寸进行图片裁剪
def crop_image(image, patch_w, patch_h):
    width, height = image.size
    # 补丁计数
    cnt = 0
    for w in range(0, width, patch_w):
        for h in range(0, height, patch_h):
            cnt += 1
            # 指定原图片的左、上、右、下
            img = image.crop((w, h, w+patch_w, h+patch_h))
            img.save("475-%d.tif" % cnt)
    print("图片补丁裁剪结束,共有{}张补丁".format(cnt))

def main():
    img_path = r"D:\数据集\1"
    img_Topath = r'D:\数据集\2'
    filelist = os.listdir(img_path)
    i = 0
    type = 'png'
    # 查看图像形状


    # 输入指定的补丁宽高
# =============================================================================
#     print("输入补丁宽高:")
#     wi, he = map(int, input().split(" "))
# =============================================================================
    for item in filelist:
        arr = item.strip().split('*')
        img_name = arr[0]
        image_path = os.path.join(img_path, img_name)
        img = Image.open(image_path)

        print("原始图像形状{}".format(np.array(img).shape))
        
        # 进行图像填充
        new_image = judge(img, 1024, 1024)
        # 图片补丁裁剪
        crop_image(new_image, 1024, 1024)
        img = new_image
        img.save(img_Topath +'/'+img_name)

if __name__ == '__main__':
    main()

修改后

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值