Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

gif.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """GIF Transform Processing."""
  2. import os
  3. import shutil
  4. import tempfile
  5. import cv2
  6. import imageio
  7. from config import Config as Conf
  8. from processing import Processing
  9. from processing.multiple import MultipleImageProcessing
  10. from processing.utils import select_phases
  11. from utils import write_image
  12. class GifProcessing(Processing):
  13. """GIF Image Processing Class."""
  14. def _setup(self, *args):
  15. self.__phases = select_phases(self._args)
  16. self.__input_path = self._args['input']
  17. self.__output_path = self._args['output']
  18. self.__tmp_dir = None
  19. self.__temp_input_paths = []
  20. self.__temp_output_paths = []
  21. self.__tmp_dir = tempfile.mkdtemp()
  22. Conf.log.debug("Temporay dir is {}".format(self.__tmp_dir))
  23. imgs = imageio.mimread(self.__input_path)
  24. Conf.log.info("GIF have {} Frames To Process".format(len(imgs)))
  25. self.__temp_input_paths = [os.path.join(self.__tmp_dir, "intput_{}.png".format(i))
  26. for i in range(len(imgs))]
  27. self._args['input'] = self.__temp_input_paths
  28. self.__temp_output_paths = [os.path.join(self.__tmp_dir, "output_{}.png".format(i))
  29. for i in range(len(imgs))]
  30. self._args['output'] = self.__temp_output_paths
  31. for i in zip(imgs, self.__temp_input_paths):
  32. write_image(cv2.cvtColor(i[0], cv2.COLOR_RGB2BGR), i[1])
  33. def _execute(self, *args):
  34. """
  35. Execute all phases on each frames of the gif and recreate the gif.
  36. :return: None
  37. """
  38. MultipleImageProcessing().run(config=self._args)
  39. dir_out = os.path.dirname(self.__output_path)
  40. if dir_out != '':
  41. os.makedirs(dir_out, exist_ok=True)
  42. imageio.mimsave(self.__output_path, [imageio.imread(i) for i in self.__temp_output_paths])
  43. Conf.log.info("{} Gif Created ".format(self.__output_path))
  44. def _clean(self, *args):
  45. shutil.rmtree(self.__tmp_dir)