diff options
| author | pepperpepperpepper <pepper@scannerjammer.com> | 2016-03-06 21:13:19 -0800 |
|---|---|---|
| committer | pepperpepperpepper <pepper@scannerjammer.com> | 2016-03-06 21:13:19 -0800 |
| commit | 8f9822ada4a231680e7a79447d4bc55ac93dbe59 (patch) | |
| tree | a7d62542ae3af4624584e853216f5504664454dc | |
| parent | 26b397735a2a61254d5aca0d70a4332ff73f13f4 (diff) | |
ok looking good
| -rw-r--r-- | photoblaster/_file.py | 30 | ||||
| -rw-r--r-- | photoblaster/config.py | 1 | ||||
| -rw-r--r-- | photoblaster/modules/pbconcat/__init__.py | 89 | ||||
| -rw-r--r-- | photoblaster/param/img_url.py | 11 | ||||
| -rw-r--r-- | photoblaster/params.py | 10 | ||||
| -rw-r--r-- | run_module_examples.py | 2 |
6 files changed, 90 insertions, 53 deletions
diff --git a/photoblaster/_file.py b/photoblaster/_file.py index 921c0ce..cf07510 100644 --- a/photoblaster/_file.py +++ b/photoblaster/_file.py @@ -6,7 +6,8 @@ import sys import time from subprocess import Popen, PIPE, call from photoblaster.config import WORKING_DIR, BIN_IDENTIFY, \ - BIN_CONVERT, BASE_URL, DEFAULT_FINALFORMAT + BIN_CONVERT, BASE_URL, DEFAULT_FINALFORMAT, BIN_GIFSICLE +from photoblaster.s3.cli import S3Cli _MAX_FILENAME_LENGTH = 20 @@ -146,12 +147,14 @@ class File(object): } def s3move(self): - from photoblaster.s3.cli import S3Cli s3cli = S3Cli() s3cli.s3move( - self.get_filepath(), "im/{}/{}".format( - self._hashdir, self.get_filename()) + self.get_filepath(), + "im/{}/{}".format( + self._hashdir, + self.get_filename() ) + ) self._storage = "s3" def delete(self): @@ -186,6 +189,25 @@ class File(object): if module: module.commands.append(" ".join(cmd)) + def optimize_gif(self, module=None): + filepath_tmp = "%s_tmp.gif" % self.get_filepath() + f = open(filepath_tmp, "w") + cmd = [ + BIN_GIFSICLE, + self.get_filepath(), + "--optimize=2", + ] + call(cmd, stdout=f) + cmd2 = ["mv", filepath_tmp, self.get_filepath()] + call(cmd2) + if module: + module.commands.append( + "%s > %s" % (" ".join(cmd), filepath_tmp) + ) + module.commands.append( + " ".join(cmd2) + ) + class FrameFile(File): def __init__( diff --git a/photoblaster/config.py b/photoblaster/config.py index 9e0bc4f..76d465c 100644 --- a/photoblaster/config.py +++ b/photoblaster/config.py @@ -10,6 +10,7 @@ MAX_SIZE = 1024 * 1024 * 1.2 * 1.5 BIN_CONVERT = "/usr/bin/convert" BIN_COMPOSITE = "/usr/bin/composite" BIN_IDENTIFY = "/usr/bin/identify" +BIN_GIFSICLE = "/usr/bin/gifsicle" _RELATIVE_DIR = os.path.dirname(__file__) diff --git a/photoblaster/modules/pbconcat/__init__.py b/photoblaster/modules/pbconcat/__init__.py index 707d04c..b8f7b69 100644 --- a/photoblaster/modules/pbconcat/__init__.py +++ b/photoblaster/modules/pbconcat/__init__.py @@ -6,7 +6,9 @@ _DEFAULT_DIRECTION = 'right' class PbConcat(ModuleBase): example_params = { - 'img_url1': "http://i.imgur.com/QGIplwA.gif", + 'img_url1': ( + "http://i.asdf.us/im/d9/PbConcat_1457326934-16_donkey.gif" + ), 'img_url2': ( "http://dump.fm/images/20100924/" "1285385385062-dumpfm-j1p2m3-animate.gif"), @@ -47,7 +49,8 @@ class PbConcat(ModuleBase): self.params.definitions_import( _definitions, kwargs, - classname=self.__class__.__name__) + classname=self.__class__.__name__, + module=self) self.set_output_file( File( classname=self.__class__.__name__, @@ -57,22 +60,32 @@ class PbConcat(ModuleBase): ) self._db_url_param = self.params.img_url1.url - def _get_frames_of_equal_count(self, image_target, image_to_alter): + def _get_frames_of_equal_count(self): if self.params.finalformat in ["png", "jpg"]: - image_target.compress_to_single_frame() - image_to_alter.compress_to_single_frame() - return ([image_target], [image_to_alter]) - to_alter_frames = image_to_alter.get_frames() - target_frames = image_target.get_frames() - if len(to_alter_frames) > len(target_frames): - frames = self._reduce_frames( - to_alter_frames, len(target_frames)) - elif len(to_alter_frames) < len(target_frames): - frames = self._add_frames( - to_alter_frames, len(target_frames)) - else: - frames = (target_frames, to_alter_frames) - return frames + self.params.img_url1.get_file().compress_to_single_frame() + self.params.img_url2.get_file().compress_to_single_frame() + return ([self.params.img_url1], [self.params.img_url2]) + im1_frames = self.params.img_url1.get_file().get_frames() + im2_frames = self.params.img_url2.get_file().get_frames() + if len(im1_frames) > len(im2_frames): + if self.params.frames_match == "shorter": + im1_frames = self._reduce_frames( + im1_frames, len(im2_frames) + ) + else: + im2_frames = self._add_frames( + im2_frames, len(im1_frames) + ) + elif len(im1_frames) < len(im2_frames): + if self.params.frames_match == "shorter": + im2_frames = self._reduce_frames( + im2_frames, len(im1_frames) + ) + else: + im1_frames = self._add_frames( + im1_frames, len(im2_frames) + ) + return (im1_frames, im2_frames) def _merge_frames_of_equal_count(self, frames1, frames2): frames = [] @@ -139,32 +152,23 @@ class PbConcat(ModuleBase): self._call_cmd(cmd) return tempfile + def _merge_all_frames(self, frames): + if len(frames) > 1 and self.params.finalformat == "gif": + self._frames_to_gif(frames) + else: + frames[0].set_filepath( + self.get_output_file().get_filepath(), + module=self + ) + def _frames_to_gif(self, frames): - gif_file = File( - is_temp=True, - classname=self.__class__.__name__, - extension="gif" - ) cmd = [BIN_CONVERT] cmd += ['-dispose', '2'] cmd += ['-loop', '0'] cmd += [frame.get_filepath() for frame in frames] - cmd += [gif_file.get_filepath()] + cmd += [self.get_output_file().get_filepath()] self._call_cmd(cmd) - return gif_file - - def _concat_gif(self, image1, image2): - merged_files = [] - frames1 = image1.get_frames() - frames2 = image2.get_frames() - if len(frames1) != len(frames2): - raise AttributeError( - "Cannot merge gifs of different frame length") - for i in xrange(0, len(frames1)): - merged_files.append( - self._concat_static(frames1[i], frames2[i]) - ) - return self._frames_to_gif(merged_files) + self.get_output_file().optimize_gif() def _add_frames(self, frames, frame_number): """ @@ -181,14 +185,9 @@ class PbConcat(ModuleBase): def create(self): self._match_dimensions() - file_created = self._merge_frames( - self.params.img_url1.get_file(), - self.params.img_url2.get_file(), - ) - file_created.set_filepath( - self.get_output_file().get_filepath(), - module=self - ) + frames1, frames2 = self._get_frames_of_equal_count() + frames_merged = self._merge_frames_of_equal_count(frames1, frames2) + self._merge_all_frames(frames_merged) super(PbConcat, self).create() def _add_canvas(self, image, canvas_color="transparent"): diff --git a/photoblaster/param/img_url.py b/photoblaster/param/img_url.py index a6227e1..4ff7423 100644 --- a/photoblaster/param/img_url.py +++ b/photoblaster/param/img_url.py @@ -9,7 +9,7 @@ from subprocess import Popen, PIPE class Img_url(Param): - def __init__(self, value, key="", classname=""): + def __init__(self, value, key="", classname="", module=None): """Defines the float param type. Takes in a url, sends a get request to the url, writes the response to a temporary filename, and checks the mimetype with imagemagick. @@ -29,10 +29,16 @@ class Img_url(Param): self._file = File( namepart=key, classname=classname, + extension="", is_temp=True + ) self._image_download(value) self.mimetype = self._image_mimetype(self.get_filepath()) + self.set_filepath( + "%s.%s" % (self.get_filepath(), self.mimetype), + module=module + ) self.url = value def __dict__(self): @@ -117,6 +123,9 @@ class Img_url(Param): def get_filepath(self): return self._file.get_filepath() + def set_filepath(self, *args, **kwargs): + return self._file.set_filepath(*args, **kwargs) + def get_file_dimensions(self): return self._file.get_dimensions() diff --git a/photoblaster/params.py b/photoblaster/params.py index 275f030..488f639 100644 --- a/photoblaster/params.py +++ b/photoblaster/params.py @@ -41,7 +41,12 @@ class Params(object): except AttributeError: return None - def definitions_import(self, def_dict, classkwargs, classname=""): + def definitions_import( + self, + def_dict, + classkwargs, + classname="", + module=None): """main method of this class. takes a dict of definitions, along with the keyword arguments of the module and maps them to attribute values of the params class""" @@ -65,7 +70,8 @@ class Params(object): elif def_dict[key]['type'] == "float": instance = Float(value, classname=classname) elif def_dict[key]['type'] == "img_url": - instance = Img_url(value, key=key, classname=classname) + instance = Img_url( + value, key=key, classname=classname, module=module) elif def_dict[key]['type'] == "int": instance = Int(value, classname=classname) elif def_dict[key]['type'] == "json": diff --git a/run_module_examples.py b/run_module_examples.py index d6900dc..27fd13c 100644 --- a/run_module_examples.py +++ b/run_module_examples.py @@ -4,7 +4,7 @@ from photoblaster.modules import Modules modules = Modules() for module_name in modules.list_modules(): - if module_name == "pbgenerate": + if module_name == "pbconcat": print "\n\n\n" print "running example for %s" % module_name cls = modules.get_module(module_name) |
