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.4KB

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