small2bigimage.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. # File : small2bigimage.py
  3. # Time :2024-04-16 21:30
  4. # Author :FEANGYANG
  5. # version :python 3.7
  6. # Contact :1071082183@qq.com
  7. # Description:
  8. """
  9. import os
  10. # import PIL.Image as Image
  11. from PIL import Image, ImageDraw
  12. def split_list_average_n(origin_list, n):
  13. for i in range(0, len(origin_list), n):
  14. yield origin_list[i:i + n]
  15. # 16*22
  16. small_images_path = '../Swin-Transformer/v.txt'
  17. label_list_path = '../Swin-Transformer/k.txt'
  18. # images_name = os.listdir(small_images_path)
  19. f = open(label_list_path, 'r')
  20. lines_label = f.readlines()
  21. f2 = open(small_images_path, 'r')
  22. _lines_image = f2.readlines()
  23. lines_image = []
  24. dic = {}
  25. for i in range(len(_lines_image)):
  26. dic[_lines_image[i].split()[0]] = lines_label[i].split(' ')[-1].split()[0]
  27. lines_image.append(_lines_image[i].split()[0])
  28. # lines_image_2d = []
  29. lines_image_3d = []
  30. for i in range(0, len(lines_image), 352):
  31. # lines_image_2d.append(lines_image[i:i+352])
  32. lines_image_2d = []
  33. for j in range(0, len(lines_image[i:i+352]), 22):
  34. lines_image_2d.append(lines_image[i+j:i+j+22])
  35. lines_image_3d.append(lines_image_2d)
  36. # lines_image_2d= split_list_average_n(lines_image, 352)
  37. # lines_image_3d = []
  38. #
  39. #
  40. #
  41. # for d in lines_image_2d:
  42. # _d = split_list_average_n(d, 16)
  43. # lines_image_3d.append(_d)
  44. #
  45. # for lines in lines_image_3d:
  46. # for line in lines:
  47. # for l in line:
  48. # print(len(l))
  49. SAVE_PATH = './result_big_images/'
  50. IMAGES_PATH = '/data/fengyang/sunwin/code/swin_conda_env/Swin-Transformer/imagenet/test/nosmoke/'
  51. IMAGE_SIZE = 128 # 每张小图片的大小
  52. IMAGE_SIZE_x=128
  53. IMAGE_ROW = 8 # 图片间隔,也就是合并成一张图后,一共有几行 *****_8_**_image.jpg
  54. IMAGE_COLUMN = 11 # *****_**_11_image.jpg
  55. if not os.path.exists(SAVE_PATH): # 判断是否存在文件夹如果不存在则创建为文件夹
  56. os.makedirs(SAVE_PATH)
  57. # def image_compose():
  58. # to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE_x, IMAGE_ROW * IMAGE_SIZE)) #创建一个新图
  59. # # 循环遍历,把每张图片按顺序粘贴到对应位置上
  60. # for y in range(1, IMAGE_ROW + 1):
  61. # for x in range(1, IMAGE_COLUMN + 1):
  62. # from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
  63. # (IMAGE_SIZE_x, IMAGE_SIZE),Image.ANTIALIAS)
  64. # to_image.paste(from_image, ((x - 1) * IMAGE_SIZE_x, (y - 1) * IMAGE_SIZE))
  65. # return to_image.save(IMAGE_SAVE_PATH) # 保存新图
  66. for i in range(len(lines_image_3d)):
  67. img_n = str(i+1).zfill(6)
  68. # img_out = cv2.imread(path)
  69. to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE_x, IMAGE_ROW * IMAGE_SIZE)) # 创建一个新图
  70. img_name = ''
  71. for y in range(1, IMAGE_ROW + 1):
  72. img_name= img_n + '_' +str(y)
  73. image_name=''
  74. for x in range(1, IMAGE_COLUMN + 1):
  75. image_name = img_name + '_' + str(x)+'_image.jpg'
  76. from_image = Image.open(IMAGES_PATH + image_name).convert('RGBA')
  77. lable = dic.get(image_name)
  78. draw = ImageDraw.Draw(from_image)
  79. # if y > 10 and x > 15:
  80. # lable = '0'
  81. # if x < 6:
  82. # lable = '0'
  83. if lable == '0':
  84. draw.rectangle([1, 1, IMAGE_SIZE-1, IMAGE_SIZE_x-1], outline='green', width=2)
  85. # draw.rectangle([tuple(text_origin1), tuple(text_origin1 + label_size1)], fill='red')
  86. else:
  87. draw.rectangle([1, 1, IMAGE_SIZE-1, IMAGE_SIZE_x-1], outline='red', width=2)
  88. # draw.rectangle([tuple(text_origin2), tuple(text_origin2 + label_size2)], fill='green')
  89. del draw
  90. # from_image.paste(Image.alpha_composite(from_image, transp)).convert('RGB')
  91. to_image.paste(from_image, ((x - 1) * IMAGE_SIZE_x, (y - 1) * IMAGE_SIZE))
  92. to_image.save(SAVE_PATH+img_n+'.jpg')