MergePic.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import os
  2. import PIL.Image as Image
  3. class ImageProcessing:
  4. """
  5. 图片加工类
  6. """
  7. def __init__(self, image_dir_path, image_size, image_row):
  8. self.image_dir_path = image_dir_path
  9. self.image_size = image_size
  10. self.image_row = image_row
  11. def merge_images(self):
  12. """
  13. 合并图片
  14. @return: image_info_dict
  15. """
  16. # 获取图片集地址下的所有图片名称
  17. image_full_path_list = self.get_image_full_path_list(self.image_dir_path)
  18. image_full_path_list.sort()
  19. image_save_path = r'{}.jpg'.format(self.image_dir_path) # 图片转换后的地址
  20. # 计算合成图片后的图片行数
  21. if len(image_full_path_list) % self.image_row == 0:
  22. image_row_num = len(image_full_path_list) // self.image_row
  23. else:
  24. image_row_num = len(image_full_path_list) // self.image_row + 1
  25. x_list = []
  26. y_list = []
  27. for img_file in image_full_path_list:
  28. img_x, img_y = self.get_new_img_xy(img_file, self.image_size)
  29. x_list.append(img_x)
  30. y_list.append(img_y)
  31. x_new = int(x_list[len(x_list) // 5 * 4])
  32. y_new = int(y_list[len(y_list) // 5 * 4])
  33. self.composite_images(self.image_row, self.image_size, image_row_num, image_full_path_list, image_save_path,
  34. x_new, y_new)
  35. image_info_dict = {'width': x_list[0], 'height': sum(y_list), 'num': len(y_list)}
  36. return image_info_dict
  37. def get_image_full_path_list(self, image_dir_path):
  38. """
  39. 获取图片完整路径列表
  40. @param image_dir_path: 图片路径
  41. @return: image_full_path_list
  42. """
  43. file_name_list = os.listdir(image_dir_path)
  44. image_full_path_list = []
  45. for file_name_one in file_name_list:
  46. file_one_path = os.path.join(image_dir_path, file_name_one)
  47. if os.path.isfile(file_one_path):
  48. image_full_path_list.append(file_one_path)
  49. else:
  50. img_path_list = self.get_image_full_path_list(file_one_path)
  51. image_full_path_list.extend(img_path_list)
  52. return image_full_path_list
  53. @staticmethod
  54. def get_new_img_xy(img_file, image_size):
  55. """
  56. 获取图片宽高像素
  57. @param img_file: 图片文件
  58. @param image_size: 图片大小
  59. @return : x_s(宽像素), y_s(高像素)
  60. """
  61. im = Image.open(img_file)
  62. if image_size == 0: # 等于0时按照原比例
  63. (x_s, y_s) = im.size
  64. return x_s, y_s
  65. else:
  66. (x, y) = im.size
  67. lv = round(x / image_size, 2) + 0.01
  68. x_s = x // lv
  69. y_s = y // lv
  70. return x_s, y_s
  71. def composite_images(self, image_row, image_size, image_row_num, image_names, image_save_path, x_new, y_new):
  72. """
  73. 合成图片
  74. @param image_row: 图片行数(合成前)
  75. @param image_size: 图片大小
  76. @param image_row_num: 图片行数(合成后)
  77. @param image_names: 图片名字
  78. @param image_save_path: 图片保存路径
  79. @param x_new: 横向位置
  80. @param y_new: 纵向位置
  81. @return: None
  82. """
  83. to_image = Image.new('RGB', (image_row * x_new, image_row_num * y_new)) # 创建一个新图
  84. # 循环遍历,把每张图片按顺序粘贴到对应位置上
  85. total_num = 0
  86. for y in range(1, image_row_num + 1):
  87. for x in range(1, image_row + 1):
  88. from_image = self.resize_by_width(image_names[image_row * (y - 1) + x - 1], image_size)
  89. to_image.paste(from_image, ((x - 1) * x_new, (y - 1) * y_new))
  90. total_num += 1
  91. if total_num == len(image_names):
  92. break
  93. to_image.save(image_save_path) # 保存新图
  94. @staticmethod
  95. def resize_by_width(infile, image_size):
  96. """按照宽度进行所需比例缩放"""
  97. im = Image.open(infile)
  98. if image_size != 0:
  99. (x, y) = im.size
  100. lv = round(x / image_size, 2) + 0.01
  101. x_s = int(x // lv)
  102. y_s = int(y // lv)
  103. print("x_s", x_s, y_s)
  104. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  105. return out
  106. else:
  107. (x_s, y_s) = im.size
  108. print("x_s", x_s, y_s)
  109. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  110. return out