""" # File : small2bigimage.py # Time :2024-04-16 21:30 # Author :FEANGYANG # version :python 3.7 # Contact :1071082183@qq.com # Description: """ import os # import PIL.Image as Image from PIL import Image, ImageDraw def split_list_average_n(origin_list, n): for i in range(0, len(origin_list), n): yield origin_list[i:i + n] # 16*22 small_images_path = '../Swin-Transformer/v.txt' label_list_path = '../Swin-Transformer/k.txt' # images_name = os.listdir(small_images_path) f = open(label_list_path, 'r') lines_label = f.readlines() f2 = open(small_images_path, 'r') _lines_image = f2.readlines() lines_image = [] dic = {} for i in range(len(_lines_image)): dic[_lines_image[i].split()[0]] = lines_label[i].split(' ')[-1].split()[0] lines_image.append(_lines_image[i].split()[0]) # lines_image_2d = [] lines_image_3d = [] for i in range(0, len(lines_image), 352): # lines_image_2d.append(lines_image[i:i+352]) lines_image_2d = [] for j in range(0, len(lines_image[i:i+352]), 22): lines_image_2d.append(lines_image[i+j:i+j+22]) lines_image_3d.append(lines_image_2d) # lines_image_2d= split_list_average_n(lines_image, 352) # lines_image_3d = [] # # # # for d in lines_image_2d: # _d = split_list_average_n(d, 16) # lines_image_3d.append(_d) # # for lines in lines_image_3d: # for line in lines: # for l in line: # print(len(l)) SAVE_PATH = './result_big_images/' IMAGES_PATH = '/data/fengyang/sunwin/code/swin_conda_env/Swin-Transformer/imagenet/test/nosmoke/' IMAGE_SIZE = 128 # 每张小图片的大小 IMAGE_SIZE_x=128 IMAGE_ROW = 8 # 图片间隔,也就是合并成一张图后,一共有几行 *****_8_**_image.jpg IMAGE_COLUMN = 11 # *****_**_11_image.jpg if not os.path.exists(SAVE_PATH): # 判断是否存在文件夹如果不存在则创建为文件夹 os.makedirs(SAVE_PATH) # def image_compose(): # to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE_x, IMAGE_ROW * IMAGE_SIZE)) #创建一个新图 # # 循环遍历,把每张图片按顺序粘贴到对应位置上 # for y in range(1, IMAGE_ROW + 1): # for x in range(1, IMAGE_COLUMN + 1): # from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize( # (IMAGE_SIZE_x, IMAGE_SIZE),Image.ANTIALIAS) # to_image.paste(from_image, ((x - 1) * IMAGE_SIZE_x, (y - 1) * IMAGE_SIZE)) # return to_image.save(IMAGE_SAVE_PATH) # 保存新图 for i in range(len(lines_image_3d)): img_n = str(i+1).zfill(6) # img_out = cv2.imread(path) to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE_x, IMAGE_ROW * IMAGE_SIZE)) # 创建一个新图 img_name = '' for y in range(1, IMAGE_ROW + 1): img_name= img_n + '_' +str(y) image_name='' for x in range(1, IMAGE_COLUMN + 1): image_name = img_name + '_' + str(x)+'_image.jpg' from_image = Image.open(IMAGES_PATH + image_name).convert('RGBA') lable = dic.get(image_name) draw = ImageDraw.Draw(from_image) # if y > 10 and x > 15: # lable = '0' # if x < 6: # lable = '0' if lable == '0': draw.rectangle([1, 1, IMAGE_SIZE-1, IMAGE_SIZE_x-1], outline='green', width=2) # draw.rectangle([tuple(text_origin1), tuple(text_origin1 + label_size1)], fill='red') else: draw.rectangle([1, 1, IMAGE_SIZE-1, IMAGE_SIZE_x-1], outline='red', width=2) # draw.rectangle([tuple(text_origin2), tuple(text_origin2 + label_size2)], fill='green') del draw # from_image.paste(Image.alpha_composite(from_image, transp)).convert('RGB') to_image.paste(from_image, ((x - 1) * IMAGE_SIZE_x, (y - 1) * IMAGE_SIZE)) to_image.save(SAVE_PATH+img_n+'.jpg')