You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

folder.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Folder Image Transform Processing."""
  2. import copy
  3. import json
  4. import os
  5. import pathlib
  6. import sys
  7. from json import JSONDecodeError
  8. from config import Config as Conf
  9. from processing.multiple import MultipleImageProcessing
  10. from processing.utils import select_phases, is_file
  11. from utils import is_a_supported_image_file_extension
  12. class FolderImageProcessing(MultipleImageProcessing):
  13. """Folder Image Processing Class."""
  14. def _setup(self, *args):
  15. self._input_folder_path = self._args['input']
  16. self._output_folder_path = self._args['output']
  17. self._multiprocessing = Conf.multiprocessing()
  18. self._process_list = []
  19. Conf.log.debug([(r, d, f) for r, d, f in os.walk(self._input_folder_path)])
  20. for r, _, _ in os.walk(self._input_folder_path):
  21. args = copy.deepcopy(self._args)
  22. args['input'] = [
  23. x.path for x in os.scandir(r) if is_file(args, x.path) and is_a_supported_image_file_extension(x.path)
  24. ]
  25. args['phases'] = select_phases(self._args)
  26. args['output'] = [
  27. "{}{}{}".format(
  28. os.path.splitext(x)[0],
  29. '_out',
  30. os.path.splitext(x)[1]
  31. )
  32. if not Conf.args['output'] else
  33. os.path.join(
  34. Conf.args['output'],
  35. pathlib.Path(*pathlib.Path(r).parts[1:]),
  36. os.path.basename(x)
  37. )
  38. for x in args['input']
  39. ]
  40. self._process_list.append(
  41. (MultipleImageProcessing(), self.__get_folder_args(args, r))
  42. )
  43. @staticmethod
  44. def __get_folder_args(args, folder_path):
  45. def add_folder_altered(args):
  46. if args.get('altered'):
  47. args['folder_altered'] = os.path.join(args['altered'],
  48. pathlib.Path(*pathlib.Path(folder_path).parts[1:]))
  49. return args
  50. json_path = os.path.join(folder_path, args['json_folder_name'])
  51. Conf.log.debug("Json Path Setting Path: {}".format(json_path))
  52. if not os.path.isfile(json_path):
  53. Conf.log.info("No Json File Settings Found In {}. Using Default Configuration. ".format(folder_path))
  54. return add_folder_altered(args)
  55. try:
  56. with open(json_path, 'r') as f:
  57. json_data = json.load(f)
  58. except JSONDecodeError:
  59. Conf.log.info("Json File Settings {} Is Not In Valid JSON Format. Using Default Configuration. "
  60. .format(folder_path))
  61. return add_folder_altered(args)
  62. try:
  63. from argv import Parser, config_args
  64. a = config_args(Parser.parser, Parser.parser.parse_args(sys.argv[1:]), json_data=json_data)
  65. Conf.log.info("Using {} Configuration for processing {} folder. "
  66. .format(json_path, folder_path))
  67. return add_folder_altered(a)
  68. except SystemExit:
  69. Conf.log.error("Arguments json file {} contains configuration error. "
  70. "Using Default Configuration".format(json_path))
  71. return add_folder_altered(args)