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.

100 lines
4.1 KiB

"""Image Transform Processing."""
import os
import sys
3 years ago
import hashlib
from config import Config as Conf
from processing import Processing
from processing.utils import select_phases
from processing.worker import run_worker
from utils import camel_case_to_str, write_image
from loader import Loader
class ImageProcessing(Processing):
"""Image Processing Class."""
def _setup(self, *args):
Process Image Constructor.
:param args: <dict> args parameter to run the image transformation (default use Conf.args)
self.__phases = select_phases(self._args)
self.__input_path = self._args['input']
self.__output_path = self._args['output']
self.__altered_path = self._args.get('altered')
self.__starting_step = self._args['steps'][0] if self._args.get('steps') else 0
self.__ending_step = self._args['steps'][1] if self._args.get('steps') else None
Conf.log.debug("All Phases : {}".format(self.__phases))
Conf.log.debug("To Be Executed Phases : {}".format(self.__phases[self.__starting_step:self.__ending_step]))
3 years ago
imagename_no_ext = os.path.splitext(os.path.basename(self.__input_path))[0]
3 years ago
if (self._args.get('folder_altered')):
3 years ago
folder_name = imagename_no_ext + '_' + str(hashlib.md5(open(self.__input_path, 'rb').read()).hexdigest())
folder_path = os.path.join(self._args['folder_altered'], folder_name)
if (not os.path.isdir(folder_path)):
os.makedirs(folder_path, exist_ok=True)
self._args['folder_altered'] = folder_path
path = self._args['folder_altered']
3 years ago
elif (self.__altered_path):
3 years ago
folder_name = imagename_no_ext + '_' + str(hashlib.md5(open(self.__input_path, 'rb').read()).hexdigest())
folder_path = os.path.join(self.__altered_path, folder_name)
if (not os.path.isdir(folder_path)):
os.makedirs(folder_path, exist_ok=True)
self.__altered_path = folder_path
3 years ago
path = self.__altered_path
self.__image_steps = [self.__input_path] + [
os.path.join(path, "{}.png".format(p().__class__.__name__))
for p in self.__phases[:self.__starting_step]
]"Processing on {}".format(str(self.__image_steps)[2:-2]))
self.__image_steps = [
(Loader.get_loader(x)).load(x) if isinstance(x, str) else x for x in self.__image_steps
3 years ago
except (FileNotFoundError, AttributeError) as e:
Conf.log.error("{} is not able to resume because it not able to load required images. "
Conf.log.error("Possible source of this error is that --altered argument is not a correct "
"directory path that contains valid images.")
def _execute(self, *args):
Execute all phases on the image.
:return: None
for p in (x for x in self.__phases[self.__starting_step:self.__ending_step]):
r = run_worker(p, self.__image_steps, config=self._args)
if self.__altered_path:
3 years ago
if (self._args.get('folder_altered')):
path = self._args['folder_altered']
path = self.__altered_path
3 years ago
write_image(r, os.path.join(path, "{}.png".format(p.__name__)))
Conf.log.debug("{} Step Image Of {} Execution".format(
3 years ago
os.path.join(path, "{}.png".format(p.__name__)),
write_image(self.__image_steps[-1], self.__output_path)"{} Created".format(self.__output_path))
Conf.log.debug("{} Result Image Of {} Execution"
.format(self.__output_path, camel_case_to_str(self.__class__.__name__)))
return self.__image_steps[-1]