extract_parts.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # main import
  2. import os
  3. import argparse
  4. import numpy as np
  5. # images imports
  6. from PIL import Image
  7. image_block = 800, 800
  8. zone_block = 200, 200
  9. def get_parts(img):
  10. parts = []
  11. h, w, c = img.shape
  12. h_block, w_block = image_block
  13. h_zone, w_zone = zone_block
  14. m_h = int(h/2)
  15. m_w = int(w/2)
  16. m_h_img_block = int(h_block/2)
  17. m_w_img_block = int(w_block/2)
  18. # extract left up image
  19. h_start = int((h % h_zone) / 2)
  20. w_start = int((w % w_zone) / 2)
  21. left_up_image = img[h_start:h_block+h_start, w_start:w_start+w_block]
  22. parts.append(left_up_image)
  23. # extract middle up image
  24. h_start = int((h % h_zone) / 2)
  25. middle_up_image = img[h_start:h_block+h_start, m_w-m_w_img_block:m_w+m_w_img_block]
  26. parts.append(middle_up_image)
  27. # extract right up image
  28. h_start = int((h % h_zone) / 2)
  29. w_end = w - int((w % w_zone) / 2)
  30. right_up_image = img[h_start:h_block+h_start, w_end-w_block:w_end]
  31. parts.append(right_up_image)
  32. # extract left bottom image
  33. h_end = h - int((h % h_zone) / 2)
  34. h_start = h_end - h_block
  35. w_start = int((w % w_zone) / 2)
  36. left_bottom_image = img[h_start:h_end, w_start:w_start+w_block]
  37. parts.append(left_bottom_image)
  38. # extract middle bottom image
  39. h_end = h - int((h % h_zone) / 2)
  40. h_start = h_end - h_block
  41. middle_bottom_image = img[h_start:h_end, m_w-m_w_img_block:m_w+m_w_img_block]
  42. parts.append(middle_bottom_image)
  43. # extract left bottom image
  44. h_end = h - int((h % h_zone) / 2)
  45. h_start = h_end - h_block
  46. w_end = w - int((w % w_zone) / 2)
  47. right_bottom_image = img[h_start:h_end, w_end-w_block:w_end]
  48. parts.append(right_bottom_image)
  49. # extract middle center image
  50. middle_image = img[m_h-m_h_img_block:m_h+m_h_img_block, m_w-m_w_img_block:m_w+m_w_img_block]
  51. parts.append(middle_image)
  52. return parts
  53. def extract(folder, output):
  54. images_path = sorted(os.listdir(folder))
  55. # for each image get sub parts
  56. for img_name in images_path:
  57. img_path = os.path.join(folder, img_name)
  58. img = np.array(Image.open(img_path))
  59. # get all expected parts from image
  60. parts = get_parts(img)
  61. for index, part in enumerate(parts):
  62. # get part output folder and create it if not exists
  63. prefix_image_folder_path = os.path.join(output, folder.replace('/', '') + '_part' + str(index))
  64. if not os.path.exists(prefix_image_folder_path):
  65. os.makedirs(prefix_image_folder_path)
  66. # build output image path and save it
  67. image_output_folder_path = os.path.join(prefix_image_folder_path, img_name)
  68. Image.fromarray(part).save(image_output_folder_path)
  69. def main():
  70. parser = argparse.ArgumentParser(description="extract and create 7 parts of image from HD one")
  71. parser.add_argument('--folder', type=str, help="folder which HD images", required=True)
  72. parser.add_argument('--output', type=str, help="output data folder", required=True)
  73. args = parser.parse_args()
  74. p_folder = args.folder
  75. p_output = args.output
  76. extract(p_folder, p_output)
  77. if __name__ == "__main__":
  78. main()