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.

image.py 3.9KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """Image Transform Processing."""
  2. import os
  3. import sys
  4. import hashlib
  5. from config import Config as Conf
  6. from processing import Processing
  7. from processing.utils import select_phases
  8. from processing.worker import run_worker
  9. from utils import camel_case_to_str, write_image
  10. from loader import Loader
  11. class ImageProcessing(Processing):
  12. """Image Processing Class."""
  13. def _setup(self, *args):
  14. """
  15. Process Image Constructor.
  16. :param args: <dict> args parameter to run the image transformation (default use Conf.args)
  17. """
  18. self.__phases = select_phases(self._args)
  19. self.__input_path = self._args['input']
  20. self.__output_path = self._args['output']
  21. self.__altered_path = self._args.get('altered')
  22. self.__starting_step = self._args['steps'][0] if self._args.get('steps') else 0
  23. self.__ending_step = self._args['steps'][1] if self._args.get('steps') else None
  24. Conf.log.debug("All Phases : {}".format(self.__phases))
  25. Conf.log.debug("To Be Executed Phases : {}".format(self.__phases[self.__starting_step:self.__ending_step]))
  26. if (self._args.get('folder_altered')):
  27. checksum_path = os.path.join(self._args['folder_altered'], str(hashlib.md5(open(self.__input_path, 'rb').read()).hexdigest()))
  28. if (not os.path.isdir(checksum_path)):
  29. os.makedirs(checksum_path, exist_ok=True)
  30. self._args['folder_altered'] = checksum_path
  31. path = checksum_path
  32. elif (self.__altered_path):
  33. checksum_path = os.path.join(self.__altered_path, str(hashlib.md5(open(self.__input_path, 'rb').read()).hexdigest()))
  34. if (not os.path.isdir(checksum_path)):
  35. os.makedirs(checksum_path, exist_ok=True)
  36. self.__altered_path = checksum_path
  37. path = self.__altered_path
  38. self.__image_steps = [self.__input_path] + [
  39. os.path.join(path, "{}.png".format(p().__class__.__name__))
  40. for p in self.__phases[:self.__starting_step]
  41. ]
  42. Conf.log.info("Processing on {}".format(str(self.__image_steps)[2:-2]))
  43. Conf.log.debug(self.__image_steps)
  44. try:
  45. self.__image_steps = [
  46. (Loader.get_loader(x)).load(x) if isinstance(x, str) else x for x in self.__image_steps
  47. ]
  48. except (FileNotFoundError, AttributeError) as e:
  49. Conf.log.error(e)
  50. Conf.log.error("{} is not able to resume because it not able to load required images. "
  51. .format(camel_case_to_str(self.__class__.__name__)))
  52. Conf.log.error("Possible source of this error is that --altered argument is not a correct "
  53. "directory path that contains valid images.")
  54. sys.exit(1)
  55. def _execute(self, *args):
  56. """
  57. Execute all phases on the image.
  58. :return: None
  59. """
  60. for p in (x for x in self.__phases[self.__starting_step:self.__ending_step]):
  61. r = run_worker(p, self.__image_steps, config=self._args)
  62. self.__image_steps.append(r)
  63. if self.__altered_path:
  64. if (self._args.get('folder_altered')):
  65. path = self._args['folder_altered']
  66. else:
  67. path = self.__altered_path
  68. write_image(r, os.path.join(path, "{}.png".format(p.__name__)))
  69. Conf.log.debug("{} Step Image Of {} Execution".format(
  70. os.path.join(path, "{}.png".format(p.__name__)),
  71. camel_case_to_str(p.__name__),
  72. ))
  73. write_image(self.__image_steps[-1], self.__output_path)
  74. Conf.log.info("{} Created".format(self.__output_path))
  75. Conf.log.debug("{} Result Image Of {} Execution"
  76. .format(self.__output_path, camel_case_to_str(self.__class__.__name__)))
  77. return self.__image_steps[-1]