extract_specific_png.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import argparse
  3. import glob
  4. def main():
  5. parser = argparse.ArgumentParser(description="Extract specific samples indices")
  6. parser.add_argument('--folder', type=str, help='folder with all rawls files', required=True)
  7. parser.add_argument('--index', type=str, help='current rawls image index', required=True)
  8. parser.add_argument('--nsamples', type=str, help='expected nsamples for image', required=True)
  9. parser.add_argument('--output', type=str, help='folder with all png files', required=True)
  10. args = parser.parse_args()
  11. p_folder = args.folder
  12. p_output = args.output
  13. p_index = args.index
  14. p_samples = args.nsamples
  15. expected_index = str(p_index)
  16. while len(expected_index) < 6:
  17. expected_index = "0" + expected_index
  18. images_path = glob.glob(f"{p_folder}/**/**/*{expected_index}.png")
  19. for img in sorted(images_path):
  20. # replace expected Samples value
  21. img_data = img.split('-')
  22. img_data[-2] = "S" + p_samples
  23. output_path = '-'.join(img_data)
  24. output_path = output_path.replace(p_folder, p_output)
  25. output_folder, _ = os.path.split(output_path)
  26. if not os.path.exists(output_folder):
  27. os.makedirs(output_folder)
  28. if not os.path.exists(output_path):
  29. os.system(f'cp {img} {output_path}')
  30. else:
  31. print(f'{output_path} already exists')
  32. if __name__ == "__main__":
  33. main()