拆分:
from PIL import Image
import os
import math
import cv2
import tifffile as tf
import time
nowtime_str = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
# print(nowtime_str)
######## 需要裁剪的图片位置#########
# path_img = "/ai32jjg/codes/data/qishi_dom/dom_car.tif"
path_img = "/ai32jjg/codes/data/qishi_dom/0819.tif"
tif_name = os.path.basename(path_img)
# mm = cv2.imread("/ai32jjg/codes/data/qishi_dom/0819.tif", cv2.COLOR_BGR2RGB)
# print(mm.shape)
# img = Image.open(path_img)
# img = tf.imread("/ai32jjg/codes/data/qishi_dom/0819.tif")
img = tf.TiffFile(path_img).asarray()
img_h, img_w, _ = img.shape
print(img_w, img_h)
suggest_wid = 1920
suggest_hig = 1280
cols = img_w // suggest_wid
rows = img_h // suggest_hig
print("rows:", rows, ", cols:", cols)
cut_wid = img_w // cols
cut_hig = img_h // rows
print("split small img is:", cut_wid, cut_hig)
# 注意这里是从上到下,再从左到右裁剪的
for k in range(cols):
x1 = k * cut_wid
for v in range(rows):
y1 = v * cut_hig
x2 = x1 + cut_wid
y2 = y1 + cut_hig
if v == (rows - 1):
y2 = img_h
if k == (cols - 1):
x2 = img_w
# print('/ai32jjg/codes/data/qishi_dom/1_caifen/'+'%d_%d' % (v, k) + '.tif --->', (x1, y1, x2, y2))
# region = img.crop((x1, y1, x2, y2))
# print(x1, y1, x2, y2)
# input()
region = img[y1:y2, x1:x2]
#####保存图片的位置以及图片名称###############
# region.save('/ai32jjg/codes/data/qishi_dom/1_caifen/'+'%d_%d' % (v, k) + '.tif')
save_dir = "/ai32jjg/codes/data/qishi_dom/" + nowtime_str + "_" + tif_name[0:-4]
if not os.path.exists(save_dir): #判断所在目录下是否有该文件名的文件夹
os.makedirs(save_dir)
# save_name = '/ai32jjg/codes/data/qishi_dom/1_caifen/'+'%d_%d' % (v, k) + '.tif'
save_name = save_dir + "/" + '%d_%d' % (v, k) + '.tif'
tf.imwrite(save_name, region)
print(save_name, "------------------->", (x1, y1, x2, y2))
print("Get geojson, please copy:", save_dir)
合并:
# 实现图像的拼接
# 两步走
# 1 先拼成一列一列的
# 2 再把拼好的列左右拼接起来
from PIL import Image
import os
import numpy as np
# 输入图像的路径
# path = "/ai32jjg/codes/data/qishi_dom/2_jiance"
# path = "/ai32jjg/codes/data/qishi_dom/1_caifen"
path = "/ai32jjg/codes/yolov5-5.0/runs/detect/ktxx6"
filenames = os.listdir(path)
print("Directory:",path)
print("Total num",len(filenames))
print('Begining...')
new_img = Image.new("RGB", (14707, 8295))
wid, hig = 2101, 1382
for filename in filenames:
print(os.path.join(path, filename))
s_img = Image.open(os.path.join(path, filename))
info = filename[0:-4].split("_")
row, col = int(info[0]), int(info[1])
new_img.paste(s_img, (wid * col, hig * row))
save_name = "/ai32jjg/codes/data/qishi_dom/dom_car_merge-2.png"
new_img.save(save_name)
print("Saved: ", save_name)
# # 定义计数的
# i = 0
# # 定义空字符串存储数组
# list_a = []
# # 1 下面的for循环用于将图像合成列,只有一个参数,就是num_yx,每列有几行图像
# for filename in filenames:
# # 定义每列有几张图像
# num_yx = 7
# # i用于计数
# i += 1
# print("This is the %d-st images." % i)
# # t用于换列
# t = (i - 1) // num_yx
# im = Image.open(os.path.join(path, filename))# 获取img
# im_array = np.array(im) # 转换为numpy数组
# # 如果取的图像输入下一列的第一个,因为每列是3张图像,所以1,4,7等就是每列的第一张
# if (i-1) % num_yx == 0:
# list_a.append(im_array)
# # 否则不是第一个数,就拼接到图像的下面
# else:
# list_a[t] = np.concatenate((list_a[t], im_array), axis=0)
# # 2 合成列以后需要将列都拼接起来
# for j in range(len(list_a)-1):
# list_a[0] = np.concatenate((list_a[0], list_a[j+1]),axis=1)
# im_save = Image.fromarray(np.uint8(list_a[0]))
# im_save.save("/ai32jjg/codes/data/qishi_dom/dom_car_merge.png")
# print("End...")