diff options
| -rw-r--r-- | photoblaster/_file.py | 28 | ||||
| -rw-r--r-- | photoblaster/modules/pbconcat/__init__.py | 75 |
2 files changed, 66 insertions, 37 deletions
diff --git a/photoblaster/_file.py b/photoblaster/_file.py index 7e1ddc0..b81de80 100644 --- a/photoblaster/_file.py +++ b/photoblaster/_file.py @@ -102,24 +102,28 @@ class File(object): f.close() return data - @staticmethod - def get_frames(filepath): + def get_frames(self): try: info = Popen( - [BIN_IDENTIFY, filepath], stdout=PIPE).communicate()[0] + [BIN_IDENTIFY, self.get_filepath()], stdout=PIPE + ).communicate()[0] frames = filter((lambda x: x), map( (lambda x: x.split(" ")[0]), (info).split('\n') )) - return frames + if len(frames) == 1: + return [self] + return map(lambda n: FrameFile(filepath=n), frames) except Exception as e: sys.stderr.write("couldn't get gif frames\n") raise e def choose_gif_frame(self, module=None): - _gif_frames = self.get_frames(self.get_filepath()) + _gif_frames = self.get_frames() + if len(_gif_frames) == 1: + return frame = random.choice(_gif_frames) - cmd = [BIN_CONVERT, frame, self.get_filepath()] + cmd = [BIN_CONVERT, frame.get_filepath(), self.get_filepath()] call(cmd) if module: module.commands.append(" ".join(cmd)) @@ -176,3 +180,15 @@ class File(object): call(cmd) if module: module.commands.append(" ".join(cmd)) + + +class FrameFile(File): + def __init__( + self, + filepath=None + ): + self._filename = os.path.basename(filepath) + self._directory = os.path.dirname(filepath) + + def as_dict(self): + return None diff --git a/photoblaster/modules/pbconcat/__init__.py b/photoblaster/modules/pbconcat/__init__.py index 9bcdec9..75d741e 100644 --- a/photoblaster/modules/pbconcat/__init__.py +++ b/photoblaster/modules/pbconcat/__init__.py @@ -5,17 +5,17 @@ _DEFAULT_MODE = 'append' #todo -#a png + a gif -#then two gifs +#handle mimetype +#make run faster class PbConcat(ModuleBase): 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", + 'img_url1': "http://i.imgur.com/QGIplwA.gif", + 'img_url2': "http://dump.fm/images/20100924/1285385385062-dumpfm-j1p2m3-animate.gif", 'username': 'donkey', 'mode': 'append', 'frames_match': 'longest', - 'finalformat': 'jpg' + 'finalformat': 'gif' } def __init__(self, **kwargs): @@ -47,7 +47,7 @@ class PbConcat(ModuleBase): self.set_output_file( File( classname=self.__class__.__name__, - extension="png", + extension=self.params.finalformat, username=self.params.username ) ) @@ -56,20 +56,25 @@ class PbConcat(ModuleBase): def _merge_frames(self, image1, image2): #order images in terms of frame count images = (image1, image2) + if self.params.finalformat in ["png", "jpg"]: + image1.choose_gif_frame() + image2.choose_gif_frame() if len(image2.get_frames()) > len(image1.get_frames()): images = (image2, image1) elif len(image2.get_frames()) == len(image1.get_frames()): + #just merge the one file 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) + #FIXME + return self._concat_gif(images[0], self._frames_to_gif(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) + return self._concat_gif(images[0], self._frames_to_gif(frames)) def _match_dimensions(self): dimensions1 = self.params.img_url1.get_file().get_dimensions() @@ -87,30 +92,40 @@ class PbConcat(ModuleBase): module=self ) # make widths the same - def _concat_static(self, imagepath1, imagepath2): + def _concat_static(self, image1, image2): tempfile = File( is_temp=True, - extension="png", + extension=self.params.finalformat, ) cmd = [] if self.params.mode == "stack": cmd = [ BIN_CONVERT, - imagepath1, imagepath2, "-append", tempfile.get_filepath()] + image1.get_filepath(), + image2.get_filepath(), + "-append", tempfile.get_filepath()] else: cmd = [ BIN_CONVERT, - imagepath1, imagepath2, "+append", tempfile.get_filepath()] + image1.get_filepath(), + image2.get_filepath(), + "+append", tempfile.get_filepath()] self._call_cmd(cmd) return tempfile - def _frames_to_gif(self, image, frames): + 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 += frames - cmd += [image.get_filepath()] + cmd += [frame.get_filepath() for frame in frames] + cmd += [gif_file.get_filepath()] self._call_cmd(cmd) + return gif_file def _concat_gif(self, image1, image2): merged_files = [] @@ -123,10 +138,7 @@ class PbConcat(ModuleBase): 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] - ) + return self._frames_to_gif(merged_files) def _add_frames(self, frames, frame_number): """ @@ -142,31 +154,32 @@ class PbConcat(ModuleBase): 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"]: - # self._add_canvas(imageparam) + if self.params.finalformat == "jpg": + for imageparam in [self.params.img_url1, self.params.img_url2]: + if getattr(imageparam, "mimetype") in ["gif", "png"]: + self._add_canvas(imageparam.get_file()) self._match_dimensions() - tempfile = self._concat_static( - self.params.img_url1.get_filepath(), - self.params.img_url2.get_filepath(), + file_created = self._merge_frames( + self.params.img_url1.get_file(), + self.params.img_url2.get_file(), ) - tempfile.set_filepath( + file_created.set_filepath( self.get_output_file().get_filepath(), module=self ) super(PbConcat, self).create() - def _add_canvas(self, imageparam): + def _add_canvas(self, image): """maybe remove""" canvasfile = File( is_temp=True, - extension=imageparam.mimetype, + extension="jpg", classname=self.__class__.__name__ ) cmd = ([ BIN_CONVERT, "-size", - "x".join(imageparam.get_file().get_dimensions()), + "x".join(image.get_dimensions()), "xc:white", canvasfile.get_filepath()]) self._call_cmd(cmd) @@ -175,9 +188,9 @@ class PbConcat(ModuleBase): BIN_COMPOSITE, "-gravity", "center", - imageparam.get_file().get_filepath(), + image.get_filepath(), canvasfile.get_filepath(), - imageparam.get_file().get_filepath()]) + image.get_filepath()]) self._call_cmd(cmd) |
