深度学习图片过大--------------实现将一张图片裁剪成二图片并合并

2019-04-15 18:50发布

import os import cv2 import numpy as np import tqdm #cv2读取一律写成默认模式 #path = 'D:\program\fast-neural-style-tensorflow-master\C\pic\' path = 'D:\program\fast-neural-style-tensorflow-master\C\style\' def different(path):#识别后的图片路径,函数主要的功能是去掉后面的_1,_2分类,然后,让后去除相同名称的,只留下文件名 all_pic = os.listdir(path) same = [] for i in all_pic: one = i.split('_')[0] same.append(one) same = list(set(same)) return same def pic_to_tow(picpath):#原始需要分割图像文件 all_pic = os.listdir(picpath) for i in tqdm.tqdm(all_pic): img = cv2.imread(picpath+i) h = int(img.shape[0]/2) w = int(img.shape[1]/2) save_path = path+'jieguo\' if not os.path.exists(save_path): os.mkdir(save_path) for k in range(1,3,1): if k == 1: cv2.imwrite(save_path+'{0}_{1}.jpg'.format(i,k),img[:,:w+20,:])#照片裁剪高和宽度 if k ==2: cv2.imwrite(save_path + '{0}_{1}.jpg'.format(i,k),img[:, w-20:, :]) # 照片裁剪高和宽度和通道数 def hebing_pic(path):#第一个参数合并识别后的图片路径,函数功能每次提取两张,合并保存 diff_file = different(path) print('合并图片') for file in tqdm.tqdm(diff_file): z = cv2.imread(path+file+'_1.jpg')#这个地方写成固定格式,可能需要修改 half_h = z.shape[0] half_w = z.shape[1] channels = z.shape[2] dst = np.zeros((half_h, half_w * 2-40,channels), np.uint8) save_path = path+'hebing\' if not os.path.exists(save_path): os.mkdir(save_path) for ii in range(1,3,1): if ii ==1: z = cv2.imread(path+file+'_'+str(ii)+'.jpg')#合并图片1 dst[:,:half_w-20,:] = z[:,:half_w-20,:] if ii ==2: z = cv2.imread(path + file + '_' + str(ii) + '.jpg') dst[:,half_w-20:,:] = z[:, 20:, :] cv2.imwrite(save_path+file,dst) #cv2.waitKey() cv2.destroyAllWindows()