裁剪分割代码
分辨率3840x2160
分辨率1920x1080
# -*- coding: utf-8 -*-
from PIL import Image
import os
import cv2
crop_w = 1024 #裁剪图像宽度
crop_h = 1024 #裁剪图像高度
imageDir="./example/images/" #./Original/Images/Labels #原大图像数据
saveDir="./example/" + str(crop_w) + "x" + str(crop_h) + "/image/" ##裁剪小图像数据
if not os.path.exists(saveDir):
os.makedirs(saveDir)
for name in os.listdir(imageDir):
img = cv2.imread(imageDir + name)
old_size= img.shape[0:2] #原图尺寸
# print(old_size[0],type(old_size[0]))
######
h_num = int(old_size[0]/crop_h) + 1 #取整后加1
w_num =int(old_size[1]/crop_w) + 1
new_height = (h_num) * crop_h #小图像尺寸整倍数的大图像
new_width = (w_num) * crop_w
print(new_height,new_width)
# #
pad_h = new_height - old_size[0] # 计算自动需要填充的像素数目(图像的高这一维度上)
pad_w = new_width - old_size[1] # 计算需要填充的像素数目(图像的宽这一维度上)
print(pad_w,pad_h)
top, bottom = pad_h // 2, pad_h - (pad_h // 2)
left, right = pad_w // 2, pad_w - (pad_w // 2)
print(top, bottom, left, right)
img_new = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, None, (0, 0, 0))
#
# kh = int(new_height/dish) - 1
# kw = int(new_width/disw) - 1
# print(kh,kw)
for i in range(h_num):
for j in range(w_num):
# print(i,j)
x = int(i * crop_h) #高度上裁剪图像个数
y = int(j * crop_w)
print(x,y)
img_crop = img_new[x : x + crop_h,y : y + crop_w]
# print(z)
saveName= name.split('.')[0] + '-' + str(i) +'-'+ str(j) +".png" #小图像名称,内含小图像的顺序
cv2.imwrite(saveDir+saveName,img_crop)
图像组合
# -*- coding: utf-8 -*-
from PIL import Image
import os, shutil
partDir="./example/320x320/image/" #小图像文件夹
biglabel = './example/images/' #原来大图像文件夹
composeDir="./example/compose/" #将一组小图像中同一个大图像下的分到同一文件夹
composesum = "./example/compose/sum" # 生成的大图像总集
if not os.path.exists(composeDir):
os.makedirs(composeDir)
if not os.path.exists(composesum):
os.makedirs(composesum)
####将分成的小图像按名称分到文件中
#读取所有裁剪图的名称
name_set = set()
for name in os.listdir(partDir):
img = Image.open(partDir + name)
w1, h1 = img.size #小图像尺寸
# print(w1,h1)
name_set.add(name.split('-')[0])
name_list = list(name_set)
print(name_list)
#生成每组小图文件夹
for i in range(len(name_list)):
print(name_list[i])
sub_path = composeDir + name_list[i] + "/"
if not os.path.exists(sub_path):
os.makedirs(sub_path)
# 移动图片到子文件夹中
for name in os.listdir(partDir):
if name.split('-')[0] == name_list[i]:
shutil.copy(partDir + name, sub_path + name)
###
#将对应子文件夹中的图像组合
for i in range(len(name_list)):
partDiri = composeDir + name_list[i] + "/" #子文件夹位置
##########
column_set = set()
for name in os.listdir(partDiri):
column_set.add(name.split('-')[2])
column = len(list(column_set))
w2 = w1 * column
raw_set = set()
for name in os.listdir(partDiri):
raw_set.add(name.split('-')[1])
raw = len(list(raw_set))
h2 = h1 * raw
total = raw * column
print(raw,column,w2,h2)
## 定义图像拼接函数 # 循环遍历,把每张图片按顺序粘贴到对应位置上
img = Image.open(biglabel + name_list[i] + '.png')
w3, h3 = img.size
print(w3,h3)
to_image = Image.new('1', (w2, h2)) # 创建一个新图
# total_num = 0
for j in range(raw):
for k in range(column):
from_image = Image.open(partDiri + name_list[i] + '-' + str(j) + '-' + str(k) + '.png')
to_image.paste(from_image, (k * w1, j * h1))
# total_num += 1
# if total_num == total:
# break
saveName = name_list[i] + '.png'
disw = (w2 - w3)/2
dish = (h2 - h3)/2
to_image_new = to_image.crop((disw, dish,disw+w3,dish+h3))
print(os.path.join(composesum,saveName))
to_image_new.save(os.path.join(composesum,saveName)) #
原创不易,感谢点赞关注!!!