MergePic.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import os
  2. import PIL.Image as Image
  3. from PIL import ImageDraw
  4. def resize_by_width(infile, image_size):
  5. """按照宽度进行所需比例缩放"""
  6. im = Image.open(infile)
  7. if image_size != 0:
  8. (x, y) = im.size
  9. lv = round(x / image_size, 2) + 0.01
  10. x_s = int(x // lv)
  11. y_s = int(y // lv)
  12. print("x_s", x_s, y_s)
  13. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  14. return out
  15. else:
  16. (x_s, y_s) = im.size
  17. print("x_s", x_s, y_s)
  18. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  19. return out
  20. def get_new_img_xy(infile, image_size):
  21. """返回一个图片的宽、高像素"""
  22. im = Image.open(infile)
  23. if image_size != 0:
  24. (x, y) = im.size
  25. lv = round(x / image_size, 2) + 0.01
  26. x_s = x // lv
  27. y_s = y // lv
  28. # print("x_s", x_s, y_s)
  29. # out = im.resize((x_s, y_s), Image.ANTIALIAS)
  30. return x_s, y_s
  31. else: #等于0时按照原比例
  32. (x_s, y_s) = im.size
  33. return x_s, y_s
  34. # 定义图像拼接函数
  35. def image_compose(image_colnum, image_size, image_rownum, image_names, image_save_path, x_new, y_new):
  36. to_image = Image.new('RGB', (image_colnum * x_new, image_rownum * y_new)) # 创建一个新图
  37. # 循环遍历,把每张图片按顺序粘贴到对应位置上
  38. total_num = 0
  39. for y in range(1, image_rownum + 1):
  40. for x in range(1, image_colnum + 1):
  41. from_image = resize_by_width(image_names[image_colnum * (y - 1) + x - 1], image_size)
  42. # from_image = Image.open(image_names[image_colnum * (y - 1) + x - 1]).resize((image_size,image_size ), Image.ANTIALIAS)
  43. to_image.paste(from_image, ((x - 1) * x_new, (y - 1) * y_new))
  44. total_num += 1
  45. if total_num == len(image_names):
  46. break
  47. return to_image.save(image_save_path) # 保存新图
  48. def get_image_list_fullpath(dir_path):
  49. file_name_list = os.listdir(dir_path)
  50. image_fullpath_list = []
  51. for file_name_one in file_name_list:
  52. file_one_path = os.path.join(dir_path, file_name_one)
  53. if os.path.isfile(file_one_path):
  54. image_fullpath_list.append(file_one_path)
  55. else:
  56. img_path_list = get_image_list_fullpath(file_one_path)
  57. image_fullpath_list.extend(img_path_list)
  58. return image_fullpath_list
  59. def merge_images(image_dir_path,image_size,image_colnum):
  60. # 获取图片集地址下的所有图片名称
  61. image_fullpath_list = get_image_list_fullpath(image_dir_path)
  62. print("image_fullpath_list", len(image_fullpath_list), image_fullpath_list)
  63. image_save_path = r'{}.jpg'.format(image_dir_path) # 图片转换后的地址
  64. print(image_save_path);
  65. # image_rownum = 4 # 图片间隔,也就是合并成一张图后,一共有几行
  66. image_rownum_yu = len(image_fullpath_list) % image_colnum
  67. if image_rownum_yu == 0:
  68. image_rownum = len(image_fullpath_list) // image_colnum
  69. else:
  70. image_rownum = len(image_fullpath_list) // image_colnum + 1
  71. x_list = []
  72. y_list = []
  73. for img_file in image_fullpath_list:
  74. img_x, img_y = get_new_img_xy(img_file, image_size)
  75. x_list.append(img_x)
  76. y_list.append(img_y)
  77. print("x_list", sorted(x_list))
  78. print("y_list", sorted(y_list))
  79. x_new = int(x_list[len(x_list) // 5 * 4])
  80. y_new = int(y_list[len(y_list) // 5 * 4])
  81. print(" x_new, y_new", x_new, y_new)
  82. image_compose(image_colnum, image_size, image_rownum, image_fullpath_list, image_save_path, x_new, y_new) # 调用函数.
  83. return {'width':x_list[0],'height':sum(y_list),'num':len(y_list)}
  84. # for img_file in image_fullpath_list:
  85. # resize_by_width(img_file,image_size)
  86. def pic_frame(image_path):
  87. im = Image.open(image_path)
  88. (x, y) = im.size
  89. width_ratio = 0.5791387557983398
  90. height_ratio = 0.23751300573349*(y*4)/y
  91. # exit(height_ratio)
  92. left_ratio = 0.24959583580493927
  93. # exit(y*0.24852335453033447)
  94. top_ratio = (y*4*0.504405677318573 - (2*y))/y
  95. # exit(top_ratio)
  96. draw = ImageDraw.Draw(im)
  97. # draw.line([(10, 10), (50, 10), (50, 50), (10, 50), (10, 10)], fill=(255, 0, 0), width=5)
  98. x1 = x*left_ratio
  99. y1 = y*top_ratio
  100. x2 = x1 + (width_ratio*x)
  101. y2 = y1
  102. x3 = x2
  103. y3 = y1 + (height_ratio*y)
  104. x4 = x1
  105. y4 = y3
  106. draw.line([(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)], fill=(255, 0, 0), width=3)
  107. im.show()
  108. if __name__ == '__main__':
  109. import PIL.Image as Image
  110. from PIL import ImageDraw
  111. import json
  112. str = '[{"Width":0.07776174694299698,"Height":0.5436824560165405,"Top":0.36302846670150757,"Left":0.38370513916015625,"picName":"1647415509_0"},{"Width":0.10947742313146591,"Height":0.4826411008834839,"Top":0.4097094237804413,"Left":0.2790755331516266,"picName":"1647415509_0"},{"Width":0.2935298979282379,"Height":0.3697746992111206,"Top":0.621006965637207,"Left":0.6143953204154968,"picName":"1647415509_3"},{"Width":0.35492533445358276,"Height":0.8629811406135559,"Top":0.08606290817260742,"Left":0.3411630690097809,"picName":"1647415509_1"},{"Width":0.39604419469833374,"Height":0.38593751192092896,"Top":0.6001746654510498,"Left":0.060247838497161865,"picName":"1647415509_3"},{"Width":0.1105344295501709,"Height":0.5028191208839417,"Top":0.3258347511291504,"Left":0.0025259912945330143,"picName":"1647415509_3"},{"Width":0.13166509568691254,"Height":0.4821750223636627,"Top":0.34587907791137695,"Left":0.10105808824300766,"picName":"1647415509_3"},{"Width":0.22752150893211365,"Height":0.3825581967830658,"Top":0.5845961570739746,"Left":0.7430258989334106,"picName":"1647415509_3"},{"Width":0.1297324150800705,"Height":0.387117862701416,"Top":0.6101043224334717,"Left":0.4607183039188385,"picName":"1647415509_3"},{"Width":0.2157023847103119,"Height":0.94093257188797,"Top":0.06487464904785156,"Left":0.26413947343826294,"picName":"1647415509_2"},{"Width":0.23323440551757812,"Height":0.8104973435401917,"Top":0.18303179740905762,"Left":0.4748744070529938,"picName":"1647415509_2"},{"Width":0.31273096799850464,"Height":0.9365510940551758,"Top":0.05696296691894531,"Left":0.6696768999099731,"picName":"1647415509_2"},{"Width":0.27037277817726135,"Height":0.8783474564552307,"Top":0.12225198745727539,"Left":0.053227268159389496,"picName":"1647415509_2"},{"Width":0.5476366281509399,"Height":0.4337165951728821,"Top":0.45788294076919556,"Left":0.38325461745262146,"picName":"1647415509_0"}]'
  113. str = json.loads(str)
  114. for label in str:
  115. im = Image.open('D:/devcode/ASJServer_test/static/ai/RBF474J66TLAGHW9111A/1647415509/'+label['picName'] + '.jpg')
  116. (x, y) = im.size
  117. width_ratio = label['Width']
  118. height_ratio = label['Height']
  119. # exit(height_ratio)
  120. left_ratio = label['Left']
  121. # exit(y*0.24852335453033447)
  122. top_ratio = label['Top']
  123. # exit(top_ratio)
  124. draw = ImageDraw.Draw(im)
  125. # draw.line([(10, 10), (50, 10), (50, 50), (10, 50), (10, 10)], fill=(255, 0, 0), width=5)
  126. x1 = x * left_ratio
  127. y1 = y * top_ratio
  128. x2 = x1 + (width_ratio * x)
  129. y2 = y1
  130. x3 = x2
  131. y3 = y1 + (height_ratio * y)
  132. x4 = x1
  133. y4 = y3
  134. draw.line([(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)], fill=(255, 0, 0), width=3)
  135. im.show()
  136. # image_path = r'D:\devcode\ASJServer_test\static\ai\RBF474J66TLAGHW9111A\1647244844.jpg' # 图片集地址
  137. image_path = r'D:\devcode\ASJServer_test\static\ai\RBF474J66TLAGHW9111A\1647244844\1647244844_0.jpg' # 图片集地址
  138. pic_frame(image_path)
  139. exit()
  140. image_dir_path = r'E:\photo\test' # 图片集地址
  141. image_size = 500 # 每张小图片的大小
  142. image_colnum = 2 # 合并成一张图后,一行有几个小图
  143. merge_images(image_dir_path, image_size, image_colnum)