spliter.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 sys
  15. import os.path as osp
  16. sys.path.insert(0, osp.abspath("..")) # add workspace
  17. import os
  18. import argparse
  19. from math import ceil
  20. from PIL import Image
  21. from paddlers.datasets.raster import Raster
  22. def split_data(image_path, block_size, save_folder):
  23. if not osp.exists(save_folder):
  24. os.makedirs(save_folder)
  25. image_name = image_path.replace("\\", "/").split("/")[-1].split(".")[0]
  26. raster = Raster(image_path, to_uint8=True)
  27. rows = ceil(raster.height / block_size)
  28. cols = ceil(raster.width / block_size)
  29. total_number = int(rows * cols)
  30. for r in range(rows):
  31. for c in range(cols):
  32. loc_start = (c * block_size, r * block_size)
  33. title = Image.fromarray(raster.getArray(loc_start, (block_size, block_size)))
  34. save_path = osp.join(save_folder, (image_name + "_" + str(r) + "_" + str(c) + ".png"))
  35. title.save(save_path, "PNG")
  36. print("-- {:d}/{:d} --".format(int(r * cols + c + 1), total_number))
  37. parser = argparse.ArgumentParser(description="input parameters")
  38. parser.add_argument("--image_path", type=str, required=True, \
  39. help="The path of big image data.")
  40. parser.add_argument("--block_size", type=int, default=512, \
  41. help="The size of image block, `512` is the default.")
  42. parser.add_argument("--save_folder", type=str, default="output", \
  43. help="The folder path to save the results, `output` is the default.")
  44. if __name__ == "__main__":
  45. args = parser.parse_args()
  46. split_data(args.image_path, args.block_size, args.save_folder)