diff options
| -rw-r--r-- | photoblaster/_file.py | 4 | ||||
| -rw-r--r-- | photoblaster/modules/pbconcat/__init__.py | 126 |
2 files changed, 74 insertions, 56 deletions
diff --git a/photoblaster/_file.py b/photoblaster/_file.py index 4343c8a..7e1ddc0 100644 --- a/photoblaster/_file.py +++ b/photoblaster/_file.py @@ -103,7 +103,7 @@ class File(object): return data @staticmethod - def gif_frames(filepath): + def get_frames(filepath): try: info = Popen( [BIN_IDENTIFY, filepath], stdout=PIPE).communicate()[0] @@ -117,7 +117,7 @@ class File(object): raise e def choose_gif_frame(self, module=None): - _gif_frames = self.gif_frames(self.get_filepath()) + _gif_frames = self.get_frames(self.get_filepath()) frame = random.choice(_gif_frames) cmd = [BIN_CONVERT, frame, self.get_filepath()] call(cmd) diff --git a/photoblaster/modules/pbconcat/__init__.py b/photoblaster/modules/pbconcat/__init__.py index f2171f5..9bcdec9 100644 --- a/photoblaster/modules/pbconcat/__init__.py +++ b/photoblaster/modules/pbconcat/__init__.py @@ -8,19 +8,15 @@ _DEFAULT_MODE = 'append' #a png + a gif #then two gifs - class PbConcat(ModuleBase): - try: - example_params = { - 'img_url1': "http://i.asdf.us/im/2e/PbConcat_1456981685_donkey.png", - 'img_url2': "http://i.asdf.us/im/6f/PbConcat_1456981587_donkey.png", - 'username': 'donkey', - 'mode': 'append', - 'frames_match': 'longest', - 'finalformat': 'jpg' - } - except: - example_params = {} + example_params = { + 'img_url1': "http://i.asdf.us/im/2e/PbConcat_1456981685_donkey.png", + 'img_url2': "http://i.asdf.us/im/6f/PbConcat_1456981587_donkey.png", + 'username': 'donkey', + 'mode': 'append', + 'frames_match': 'longest', + 'finalformat': 'jpg' + } def __init__(self, **kwargs): super(PbConcat, self).__init__(**kwargs) @@ -57,6 +53,24 @@ class PbConcat(ModuleBase): ) self._db_url_param = self.params.img_url1.url + def _merge_frames(self, image1, image2): + #order images in terms of frame count + images = (image1, image2) + if len(image2.get_frames()) > len(image1.get_frames()): + images = (image2, image1) + elif len(image2.get_frames()) == len(image1.get_frames()): + return self._concat_static(image1, image2) + if self.params.frames_match == "longest": + #additive + frames = self._add_frames( + images[1].get_frames(), len(images[0].get_frames())) + self._frames_to_gif(images[1], frames) + elif self.params.frames_match == "shortest": + #subtractive + frames = self._reduce_frames( + images[0].get_frames(), len(images[1].get_frames())) + self._frames_to_gif(images[0], frames) + def _match_dimensions(self): dimensions1 = self.params.img_url1.get_file().get_dimensions() dimensions2 = self.params.img_url2.get_file().get_dimensions() @@ -73,7 +87,7 @@ class PbConcat(ModuleBase): module=self ) # make widths the same - def _concat_static(self, image1, image2): + def _concat_static(self, imagepath1, imagepath2): tempfile = File( is_temp=True, extension="png", @@ -82,47 +96,39 @@ class PbConcat(ModuleBase): if self.params.mode == "stack": cmd = [ BIN_CONVERT, - image1, image2, "-append", tempfile.get_filepath()] + imagepath1, imagepath2, "-append", tempfile.get_filepath()] else: cmd = [ BIN_CONVERT, - image1, image2, "+append", tempfile.get_filepath()] + imagepath1, imagepath2, "+append", tempfile.get_filepath()] self._call_cmd(cmd) return tempfile - def _frames_to_gif(self, frames): + def _frames_to_gif(self, image, frames): cmd = [BIN_CONVERT] cmd += ['-dispose', '2'] cmd += ['-loop', '0'] cmd += frames - cmd += [self.get_output_file().get_filepath()] + cmd += [image.get_filepath()] self._call_cmd(cmd) - def _add_canvas(self, imageparam): - """maybe remove""" - canvasfile = File( - is_temp=True, - extension=imageparam.mimetype, - classname=self.__class__.__name__ + 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( + self.get_output_file, + [framefile.get_filepath() for framefile in merged_files] ) - cmd = ([ - BIN_CONVERT, - "-size", - "x".join(imageparam.get_file().get_dimensions()), - "xc:white", - canvasfile.get_filepath()]) - self._call_cmd(cmd) - self._files_created.append(canvasfile) - cmd = ([ - BIN_COMPOSITE, - "-gravity", - "center", - imageparam.get_file().get_filepath(), - canvasfile.get_filepath(), - imageparam.get_file().get_filepath()]) - self._call_cmd(cmd) - def add_frames(frames, frame_number): + def _add_frames(self, frames, frame_number): """ adds frames to a gif to make it the same length as another gif """ @@ -132,6 +138,9 @@ class PbConcat(ModuleBase): new_frames += frames[0:(frame_number % len(frames))] return new_frames + def _reduce_frames(self, frames, frame_number): + return frames[0:frame_number] + def create(self): #for imageparam in [self.params.img_url1, self.params.img_url2]: # if getattr(imageparam, "mimetype") in ["gif", "png"]: @@ -147,20 +156,29 @@ class PbConcat(ModuleBase): ) super(PbConcat, self).create() -# def run_concat(image1, image2): -# frames1 = gif_frames(image1) -# frames2 = gif_frames(image2) -# if len(frames1) == 1 and len(frames2) == 1: -# return concat_static(image1, image2) -# if len(frames1) > len(frames2): -# frames2 = add_frames(frames2, len(frames1)) -# elif len(frames1) < len(frames2): -# frames1 = add_frames(frames1, len(frames2)) -# final_frames = [] -# for i in xrange(0, len(frames1)): -# final_frames.append(concat_static(frames1[i], frames2[i])) -# OUTFILE = frames_to_gif(final_frames) -# return OUTFILE + def _add_canvas(self, imageparam): + """maybe remove""" + canvasfile = File( + is_temp=True, + extension=imageparam.mimetype, + classname=self.__class__.__name__ + ) + cmd = ([ + BIN_CONVERT, + "-size", + "x".join(imageparam.get_file().get_dimensions()), + "xc:white", + canvasfile.get_filepath()]) + self._call_cmd(cmd) + self._files_created.append(canvasfile) + cmd = ([ + BIN_COMPOSITE, + "-gravity", + "center", + imageparam.get_file().get_filepath(), + canvasfile.get_filepath(), + imageparam.get_file().get_filepath()]) + self._call_cmd(cmd) def get_class(): |
