6 changed files with 298 additions and 263 deletions
@ -1,58 +0,0 @@
@@ -1,58 +0,0 @@
|
||||
import os |
||||
import shutil |
||||
import tempfile |
||||
|
||||
import cv2 |
||||
import imageio |
||||
|
||||
from config import Config as conf |
||||
from processing import Process |
||||
from processing.image import MultipleImageTransform |
||||
from utils import write_image |
||||
|
||||
|
||||
class SimpleGIFTransform(Process): |
||||
""" |
||||
GIF Image Processing Class |
||||
""" |
||||
|
||||
def __init__(self, input_path, phases, output_path): |
||||
""" |
||||
ImageTransformGIF Constructor |
||||
:param images: <string> gif path to process |
||||
:param output_path: <string> image path to write the result |
||||
:param phases: <ImageTransform[]> list of transformation use by the process each image |
||||
""" |
||||
super().__init__() |
||||
self.__phases = phases |
||||
self.__input_path = input_path |
||||
self.__output_path = output_path |
||||
self.__tmp_dir = None |
||||
self.__temp_input_paths = [] |
||||
self.__temp_output_paths = [] |
||||
|
||||
def setup(self): |
||||
self.__tmp_dir = tempfile.mkdtemp() |
||||
conf.log.debug("Temporay dir is {}".format(self.__tmp_dir)) |
||||
imgs = imageio.mimread(self.__input_path) |
||||
conf.log.info("GIF have {} Frames To Process".format(len(imgs))) |
||||
self.__temp_input_paths = [os.path.join(self.__tmp_dir, "intput_{}.png".format(i)) |
||||
for i in range(len(imgs))] |
||||
|
||||
self.__temp_output_paths = [os.path.join(self.__tmp_dir, "output_{}.png".format(i)) |
||||
for i in range(len(imgs))] |
||||
|
||||
[write_image(cv2.cvtColor(i[0], cv2.COLOR_RGB2BGR), i[1]) for i in zip(imgs, self.__temp_input_paths)] |
||||
|
||||
def execute(self): |
||||
""" |
||||
Execute all phases on each frames of the gif and recreate the gif |
||||
:return: None |
||||
""" |
||||
MultipleImageTransform(self.__temp_input_paths, self.__phases, self.__temp_output_paths).run() |
||||
|
||||
imageio.mimsave(self.__output_path, [imageio.imread(i) for i in self.__temp_output_paths]) |
||||
conf.log.info("{} Gif Created ".format(self.__output_path)) |
||||
|
||||
def clean(self): |
||||
shutil.rmtree(self.__tmp_dir) |
@ -1,119 +0,0 @@
@@ -1,119 +0,0 @@
|
||||
import os |
||||
import sys |
||||
from multiprocessing.pool import ThreadPool |
||||
|
||||
from config import Config as conf |
||||
from processing import Process |
||||
from utils import read_image, write_image, camel_case_to_str |
||||
|
||||
|
||||
class SimpleImageTransform(Process): |
||||
""" |
||||
Simple Image Processing Class |
||||
""" |
||||
|
||||
def __init__(self, input_path, phases, output_path): |
||||
""" |
||||
ProcessImage Constructor |
||||
:param input_path: <string> original image path to process |
||||
:param output_path: <string> image path to write the result. |
||||
:param phases: <ImageTransform[]> list of transformation each image |
||||
""" |
||||
super().__init__() |
||||
self.__phases = phases |
||||
self.__output_path = output_path |
||||
self.__altered_path = conf.args['altered'] |
||||
self.__starting_step = conf.args['steps'][0] if conf.args['steps'] else 0 |
||||
self.__ending_step = conf.args['steps'][1] if conf.args['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])) |
||||
|
||||
self.__image_steps = [input_path] + [ |
||||
os.path.join(self.__altered_path, "{}.png".format(p.__class__.__name__)) |
||||
for p in self.__phases[:self.__starting_step] |
||||
] |
||||
|
||||
def info_start_run(self): |
||||
super().info_start_run() |
||||
conf.log.debug("Processing on {}".format(self.__image_steps)) |
||||
|
||||
def setup(self): |
||||
try: |
||||
self.__image_steps = [read_image(x) if isinstance(x, str) else x for x in self.__image_steps] |
||||
except FileNotFoundError as e: |
||||
conf.log.error(e) |
||||
conf.log.error("{} is not able to resume because it not able to load required images. " |
||||
.format(camel_case_to_str(self.__class__.__name__))) |
||||
conf.log.error("Possible source of this error is that --altered argument is not a correct " |
||||
"directory path that contains valid images.") |
||||
sys.exit(1) |
||||
|
||||
def execute(self): |
||||
""" |
||||
Execute all phases on the image |
||||
:return: None |
||||
""" |
||||
for p in self.__phases[len(self.__image_steps) - 1:]: |
||||
r = p.run(*[self.__image_steps[i] for i in p.input_index]) |
||||
self.__image_steps.append(r) |
||||
|
||||
if self.__altered_path: |
||||
write_image(r, os.path.join(self.__altered_path, "{}.png".format(p.__class__.__name__))) |
||||
conf.log.debug("Writing {}, Result of the Execution of {}" |
||||
.format( |
||||
os.path.join(self.__altered_path, "{}.png".format(p.__class__.__name__)), |
||||
camel_case_to_str(p.__class__.__name__), |
||||
)) |
||||
|
||||
write_image(self.__image_steps[-1], self.__output_path) |
||||
conf.log.debug("Writing {}, Result Image Of {} Execution" |
||||
.format(self.__output_path, camel_case_to_str(self.__class__.__name__))) |
||||
|
||||
return self.__image_steps[-1] |
||||
|
||||
|
||||
class MultipleImageTransform(Process): |
||||
""" |
||||
Multiple Image Processing Class |
||||
""" |
||||
|
||||
def __init__(self, input_paths, phases, output_paths, children_process=SimpleImageTransform): |
||||
""" |
||||
ProcessMultipleImages Constructor |
||||
:param input_paths: <string[]> images path list to process |
||||
:param output_paths: <string> images path to write the result |
||||
:param children_process: <ImageTransform> Process to use on the list of input |
||||
:param phases: <ImageTransform[]> list of transformation use by the process each image |
||||
""" |
||||
super().__init__() |
||||
self.__phases = phases |
||||
self.__input_paths = input_paths |
||||
self.__output_paths = output_paths |
||||
self.__process_list = [] |
||||
self.__multiprocessing = conf.multiprocessing() |
||||
self.__children_process = children_process |
||||
|
||||
def setup(self): |
||||
self.__process_list = [self.__children_process(i[0], self.__phases, i[1]) |
||||
for i in zip(self.__input_paths, self.__output_paths)] |
||||
|
||||
def execute(self): |
||||
""" |
||||
Execute all phases on the list of images |
||||
:return: None |
||||
""" |
||||
|
||||
def process_one_image(a): |
||||
conf.log.info("Processing image : {}/{}".format(a[1] + 1, len(self.__process_list))) |
||||
a[0].run() |
||||
|
||||
if not self.__multiprocessing: |
||||
for x in zip(self.__process_list, range(len(self.__process_list))): |
||||
process_one_image(x) |
||||
else: |
||||
conf.log.debug("Using Multiprocessing") |
||||
pool = ThreadPool(conf.args['n_cores']) |
||||
pool.map(process_one_image, zip(self.__process_list, range(len(self.__process_list)))) |
||||
pool.close() |
||||
pool.join() |
Loading…
Reference in new issue