MergePic.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import os
  2. import PIL.Image as Image
  3. from PIL import ImageDraw
  4. import logging
  5. def resize_by_width(infile, image_size):
  6. """按照宽度进行所需比例缩放"""
  7. im = Image.open(infile)
  8. if image_size != 0:
  9. (x, y) = im.size
  10. lv = round(x / image_size, 2) + 0.01
  11. x_s = int(x // lv)
  12. y_s = int(y // lv)
  13. print("x_s", x_s, y_s)
  14. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  15. return out
  16. else:
  17. (x_s, y_s) = im.size
  18. print("x_s", x_s, y_s)
  19. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  20. return out
  21. def get_new_img_xy(infile, image_size):
  22. """返回一个图片的宽、高像素"""
  23. im = Image.open(infile)
  24. if image_size != 0:
  25. (x, y) = im.size
  26. lv = round(x / image_size, 2) + 0.01
  27. x_s = x // lv
  28. y_s = y // lv
  29. # print("x_s", x_s, y_s)
  30. # out = im.resize((x_s, y_s), Image.ANTIALIAS)
  31. return x_s, y_s
  32. else: #等于0时按照原比例
  33. (x_s, y_s) = im.size
  34. return x_s, y_s
  35. # 定义图像拼接函数
  36. def image_compose(image_colnum, image_size, image_rownum, image_names, image_save_path, x_new, y_new):
  37. to_image = Image.new('RGB', (image_colnum * x_new, image_rownum * y_new)) # 创建一个新图
  38. # 循环遍历,把每张图片按顺序粘贴到对应位置上
  39. total_num = 0
  40. for y in range(1, image_rownum + 1):
  41. for x in range(1, image_colnum + 1):
  42. from_image = resize_by_width(image_names[image_colnum * (y - 1) + x - 1], image_size)
  43. # from_image = Image.open(image_names[image_colnum * (y - 1) + x - 1]).resize((image_size,image_size ), Image.ANTIALIAS)
  44. to_image.paste(from_image, ((x - 1) * x_new, (y - 1) * y_new))
  45. total_num += 1
  46. if total_num == len(image_names):
  47. break
  48. return to_image.save(image_save_path) # 保存新图
  49. def get_image_list_fullpath(dir_path):
  50. file_name_list = os.listdir(dir_path)
  51. image_fullpath_list = []
  52. for file_name_one in file_name_list:
  53. file_one_path = os.path.join(dir_path, file_name_one)
  54. if os.path.isfile(file_one_path):
  55. image_fullpath_list.append(file_one_path)
  56. else:
  57. img_path_list = get_image_list_fullpath(file_one_path)
  58. image_fullpath_list.extend(img_path_list)
  59. return image_fullpath_list
  60. def merge_images(image_dir_path,image_size,image_colnum):
  61. logger = logging.getLogger('info')
  62. # 获取图片集地址下的所有图片名称
  63. image_fullpath_list = get_image_list_fullpath(image_dir_path)
  64. image_fullpath_list.sort()
  65. logger.info('-----------into merge_images check image_fullpath_list')
  66. logger.info(image_fullpath_list)
  67. # exit(image_fullpath_list)
  68. print("image_fullpath_list", len(image_fullpath_list), image_fullpath_list)
  69. image_save_path = r'{}.jpg'.format(image_dir_path) # 图片转换后的地址
  70. print(image_save_path);
  71. # image_rownum = 4 # 图片间隔,也就是合并成一张图后,一共有几行
  72. image_rownum_yu = len(image_fullpath_list) % image_colnum
  73. if image_rownum_yu == 0:
  74. image_rownum = len(image_fullpath_list) // image_colnum
  75. else:
  76. image_rownum = len(image_fullpath_list) // image_colnum + 1
  77. x_list = []
  78. y_list = []
  79. for img_file in image_fullpath_list:
  80. img_x, img_y = get_new_img_xy(img_file, image_size)
  81. x_list.append(img_x)
  82. y_list.append(img_y)
  83. print("x_list", sorted(x_list))
  84. print("y_list", sorted(y_list))
  85. x_new = int(x_list[len(x_list) // 5 * 4])
  86. y_new = int(y_list[len(y_list) // 5 * 4])
  87. print(" x_new, y_new", x_new, y_new)
  88. image_compose(image_colnum, image_size, image_rownum, image_fullpath_list, image_save_path, x_new, y_new) # 调用函数.
  89. return {'width':x_list[0],'height':sum(y_list),'num':len(y_list)}
  90. # for img_file in image_fullpath_list:
  91. # resize_by_width(img_file,image_size)
  92. def pic_frame(image_path):
  93. im = Image.open(image_path)
  94. (x, y) = im.size
  95. width_ratio = 0.5791387557983398
  96. height_ratio = 0.23751300573349*(y*4)/y
  97. # exit(height_ratio)
  98. left_ratio = 0.24959583580493927
  99. # exit(y*0.24852335453033447)
  100. top_ratio = (y*4*0.504405677318573 - (2*y))/y
  101. # exit(top_ratio)
  102. draw = ImageDraw.Draw(im)
  103. # draw.line([(10, 10), (50, 10), (50, 50), (10, 50), (10, 10)], fill=(255, 0, 0), width=5)
  104. x1 = x*left_ratio
  105. y1 = y*top_ratio
  106. x2 = x1 + (width_ratio*x)
  107. y2 = y1
  108. x3 = x2
  109. y3 = y1 + (height_ratio*y)
  110. x4 = x1
  111. y4 = y3
  112. draw.line([(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)], fill=(255, 0, 0), width=3)
  113. im.show()
  114. if __name__ == '__main__':
  115. import PIL.Image as Image
  116. from PIL import ImageDraw
  117. import json
  118. str = '{"file_0":[{"Width":0.1756124496459961,"Height":0.19626516103744507,"Top":0.7157560586929321,"Left":0.5398021936416626},{"Width":0.07248549163341522,"Height":0.06120368093252182,"Top":0.9989035725593567,"Left":0.7263638973236084},{"Width":0.08033709228038788,"Height":0.05730639398097992,"Top":0.0012452681548893452,"Left":0.7212769985198975},{"Width":0.0820518508553505,"Height":0.0854501947760582,"Top":0.2064758688211441,"Left":0.485286146402359},{"Width":0.05153610557317734,"Height":0.07927924394607544,"Top":0.37273716926574707,"Left":0.0917201042175293},{"Width":0.0806659683585167,"Height":0.0720946192741394,"Top":0.08823304623365402,"Left":0.5998568534851074},{"Width":0.0257689468562603,"Height":0.09389368444681168,"Top":0.5453822612762451,"Left":0.6434875726699829},{"Width":0.015949059277772903,"Height":0.0916440337896347,"Top":0.5554134845733643,"Left":0.7340391874313354}],"file_1":[{"Width":0.1793510466814041,"Height":0.1985948383808136,"Top":0.7142194509506226,"Left":0.5382133722305298},{"Width":0.07374028861522675,"Height":0.06191982701420784,"Top":0.9988375902175903,"Left":0.7259079813957214},{"Width":0.08006057888269424,"Height":0.07217763364315033,"Top":0.08562219142913818,"Left":0.5998387336730957},{"Width":0.03472089767456055,"Height":0.10424872487783432,"Top":0.5323984622955322,"Left":0.712793231010437}],"file_2":[{"Width":0.17367419600486755,"Height":0.19108445942401886,"Top":0.7145259380340576,"Left":0.5396572351455688},{"Width":0.074335478246212,"Height":0.06117350235581398,"Top":0.9986345767974854,"Left":0.725648045539856},{"Width":0.08169088512659073,"Height":0.07204793393611908,"Top":0.08614397048950195,"Left":0.598838210105896},{"Width":0.08198709785938263,"Height":0.10042453557252884,"Top":0.21100950241088867,"Left":0.48813098669052124},{"Width":0.020618628710508347,"Height":0.09485914558172226,"Top":0.5609750747680664,"Left":0.5752671957015991}],"file_3":[{"Width":0.17078885436058044,"Height":0.1982082724571228,"Top":0.7157688140869141,"Left":0.5407952666282654},{"Width":0.08152589946985245,"Height":0.07218559831380844,"Top":0.08564376831054688,"Left":0.5996308326721191},{"Width":0.01685304567217827,"Height":0.09796549379825592,"Top":0.50201416015625,"Left":0.7796969413757324},{"Width":0.017284583300352097,"Height":0.09194132685661316,"Top":0.5535471439361572,"Left":0.7339507937431335}]}'
  119. str = json.loads(str)
  120. for index, labels in str.items():
  121. suffix = index.split('_')
  122. file_path = r"E:/test---------------/1647913280_"+suffix[1] + '.jpeg'
  123. im = Image.open(file_path)
  124. for label in labels:
  125. (x, y) = im.size
  126. width_ratio = label['Width']
  127. height_ratio = label['Height']
  128. # exit(height_ratio)
  129. left_ratio = label['Left']
  130. # exit(y*0.24852335453033447)
  131. top_ratio = label['Top']
  132. # exit(top_ratio)
  133. draw = ImageDraw.Draw(im)
  134. # draw.line([(10, 10), (50, 10), (50, 50), (10, 50), (10, 10)], fill=(255, 0, 0), width=5)
  135. x1 = x * left_ratio
  136. y1 = y * top_ratio
  137. x2 = x1 + (width_ratio * x)
  138. y2 = y1
  139. x3 = x2
  140. y3 = y1 + (height_ratio * y)
  141. x4 = x1
  142. y4 = y3
  143. draw.line([(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)], fill=(255, 0, 0), width=3)
  144. im.show()
  145. exit()
  146. # image_path = r'D:\devcode\ASJServer_test\static\ai\RBF474J66TLAGHW9111A\1647244844.jpg' # 图片集地址
  147. image_path = r'D:\devcode\ASJServer_test\static\ai\RBF474J66TLAGHW9111A\1647244844\1647244844_0.jpg' # 图片集地址
  148. pic_frame(image_path)
  149. exit()
  150. image_dir_path = r'E:\photo\test' # 图片集地址
  151. image_size = 500 # 每张小图片的大小
  152. image_colnum = 2 # 合并成一张图后,一行有几个小图
  153. merge_images(image_dir_path, image_size, image_colnum)