spliter.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import os.path as osp
  16. import argparse
  17. from math import ceil
  18. from PIL import Image
  19. from utils import Raster, Timer
  20. @Timer
  21. def split_data(image_path, mask_path, block_size, save_folder):
  22. if not osp.exists(save_folder):
  23. os.makedirs(save_folder)
  24. os.makedirs(osp.join(save_folder, "images"))
  25. if mask_path is not None:
  26. os.makedirs(osp.join(save_folder, "masks"))
  27. image_name = image_path.replace("\\", "/").split("/")[-1].split(".")[0]
  28. image = Raster(image_path, to_uint8=True)
  29. mask = Raster(mask_path) if mask_path is not None else None
  30. if image.width != mask.width or image.height != mask.height:
  31. raise ValueError("image's shape must equal mask's shape.")
  32. rows = ceil(image.height / block_size)
  33. cols = ceil(image.width / block_size)
  34. total_number = int(rows * cols)
  35. for r in range(rows):
  36. for c in range(cols):
  37. loc_start = (c * block_size, r * block_size)
  38. image_title = Image.fromarray(image.getArray(
  39. loc_start, (block_size, block_size))).convert("RGB")
  40. image_save_path = osp.join(save_folder, "images", (
  41. image_name + "_" + str(r) + "_" + str(c) + ".jpg"))
  42. image_title.save(image_save_path, "JPEG")
  43. if mask is not None:
  44. mask_title = Image.fromarray(mask.getArray(
  45. loc_start, (block_size, block_size))).convert("L")
  46. mask_save_path = osp.join(save_folder, "masks", (
  47. image_name + "_" + str(r) + "_" + str(c) + ".png"))
  48. mask_title.save(mask_save_path, "PNG")
  49. print("-- {:d}/{:d} --".format(int(r * cols + c + 1), total_number))
  50. parser = argparse.ArgumentParser(description="input parameters")
  51. parser.add_argument("--image_path", type=str, required=True, \
  52. help="The path of big image data.")
  53. parser.add_argument("--mask_path", type=str, default=None, \
  54. help="The path of big image label data.")
  55. parser.add_argument("--block_size", type=int, default=512, \
  56. help="The size of image block, `512` is the default.")
  57. parser.add_argument("--save_folder", type=str, default="dataset", \
  58. help="The folder path to save the results, `dataset` is the default.")
  59. if __name__ == "__main__":
  60. args = parser.parse_args()
  61. split_data(args.image_path, args.mask_path, args.block_size, args.save_folder)