def state_processing_do(self, cfg, app): idx = app.capture_choices.index(app.capture_nbr) with timeit("Creating the final picture"): captures = app.camera.get_captures() factory = get_picture_factory(captures, cfg.get('PICTURE', 'orientation')) self._pm.hook.pibooth_setup_picture_factory(cfg=cfg, opt_index=idx, factory=factory) app.previous_picture = factory.build() savedir = cfg.getpath('GENERAL', 'directory') app.previous_picture_file = osp.join( savedir, osp.basename(app.dirname) + "_pibooth.jpg") factory.save(app.previous_picture_file) if cfg.getboolean('WINDOW', 'animate') and app.capture_nbr > 1: with timeit("Asyncronously generate pictures for animation"): for capture in captures: factory = get_picture_factory( (capture, ), cfg.get('PICTURE', 'orientation'), force_pil=True) self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=factory) self.factory_pool.add(factory)
def state_processing_do(self, cfg, app): idx = app.capture_choices.index(app.capture_nbr) with timeit("Saving raw captures"): captures = app.camera.get_captures() for savedir in cfg.gettuple('GENERAL', 'directory', 'path'): rawdir = osp.join(savedir, "raw", app.capture_date) os.makedirs(rawdir) for capture in captures: count = captures.index(capture) img_path = osp.join(rawdir, "pibooth{:03}.jpg".format(count)) capture.save(img_path) # Color Filtering... for capture in captures: count = captures.index(capture) filter_count = 1 for filterName in cfg.gettyped('FILTERS', 'filters_list'): print("Filter " + filterName) filter_controller.doFilter( filterName, capture, osp.join( rawdir, "pibooth{:03}-f{}.jpg".format( count, filter_count))) filter_count = filter_count + 1 if filter_count > 3: break # 3 filters max allowed break # Only preview on the first picture for performance with timeit("Creating the final picture"): default_factory = get_picture_factory( captures, cfg.get('PICTURE', 'orientation')) factory = self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=default_factory) app.previous_picture = Image.open( '/home/pi/pibooth/assets/test.gif') #factory.build() for savedir in cfg.gettuple('GENERAL', 'directory', 'path'): app.previous_picture_file = osp.join(savedir, app.picture_filename) factory.save(app.previous_picture_file) if cfg.getboolean('WINDOW', 'animate') and app.capture_nbr > 1: with timeit("Asyncronously generate pictures for animation"): for capture in captures: default_factory = get_picture_factory( (capture, ), cfg.get('PICTURE', 'orientation'), force_pil=True, dpi=200) factory = self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=default_factory) factory.set_margin(factory._margin // 3) # 1/3 since DPI is divided by 3 self.factory_pool.add(factory)
def print_file(self, filename, copies=1): """Send a file to the CUPS server to the default printer. """ if not self.name: raise EnvironmentError( "No printer found (check config file or CUPS config)") if not osp.isfile(filename): raise IOError("No such file or directory: {}".format(filename)) if self._notifier and not self._notifier.is_subscribed(self._on_event): self._notifier.subscribe(self._on_event, [ event.CUPS_EVT_JOB_COMPLETED, event.CUPS_EVT_JOB_CREATED, event.CUPS_EVT_JOB_STOPPED, event.CUPS_EVT_PRINTER_STATE_CHANGED, event.CUPS_EVT_PRINTER_STOPPED ]) if copies > 1: with tempfile.NamedTemporaryFile( suffix=osp.basename(filename)) as fp: picture = Image.open(filename) factory = get_picture_factory((picture, ) * copies) # Don't call setup factory hook here, as the selected parameters # are the one necessary to render several pictures on same page. factory.set_margin(2) factory.save(fp.name) self._conn.printFile(self.name, fp.name, osp.basename(filename), {}) else: self._conn.printFile(self.name, filename, osp.basename(filename), {}) LOGGER.debug("File '%s' sent to the printer", filename) self.nbr_printed += 1
def regenerate_all_images(plugin_manager, config, basepath): """Regenerate the pibboth images from the raw images and the config. """ if not osp.isdir(osp.join(basepath, 'raw')): return capture_choices = config.gettuple('PICTURE', 'captures', int, 2) for captures_folder in os.listdir(osp.join(basepath, 'raw')): captures_folder_path = osp.join(basepath, 'raw', captures_folder) if not osp.isdir(captures_folder_path): continue captures = get_captures(captures_folder_path) LOGGER.info("Generating image from raws in folder %s", captures_folder_path) if len(captures) == capture_choices[0]: idx = 0 elif len(captures) == capture_choices[1]: idx = 1 else: LOGGER.warning( "Folder %s doesn't contain the correct number of pictures", captures_folder_path) continue default_factory = get_picture_factory( captures, config.get('PICTURE', 'orientation')) factory = plugin_manager.hook.pibooth_setup_picture_factory( cfg=config, opt_index=idx, factory=default_factory) picture_file = osp.join(basepath, captures_folder + "_pibooth.jpg") factory.save(picture_file)
def regenerate_all_images(config): """Regenerate the pibboth images from the raw images and the config """ captures_folders = config.getpath('GENERAL', 'directory') capture_choices = config.gettuple('PICTURE', 'captures', int, 2) backgrounds = config.gettuple('PICTURE', 'backgrounds', ('color', 'path'), 2) overlays = config.gettuple('PICTURE', 'overlays', 'path', 2) texts = [ config.get('PICTURE', 'footer_text1').strip('"'), config.get('PICTURE', 'footer_text2').strip('"') ] colors = config.gettuple('PICTURE', 'text_colors', 'color', len(texts)) text_fonts = config.gettuple('PICTURE', 'text_fonts', str, len(texts)) alignments = config.gettuple('PICTURE', 'text_alignments', str, len(texts)) # Part that fetch the captures for captures_folder in os.listdir(osp.join(captures_folders, 'raw')): captures_folder_path = osp.join(captures_folders, 'raw', captures_folder) if not osp.isdir(captures_folder_path): continue captures = get_captures(captures_folder_path) LOGGER.info("Generating image from raws in folder %s", captures_folder_path) if len(captures) == capture_choices[0]: overlay = overlays[0] background = backgrounds[0] elif len(captures) == capture_choices[1]: overlay = overlays[1] background = backgrounds[1] else: LOGGER.warning( "Folder %s doesn't contain the correct number of pictures", captures_folder_path) continue factory = get_picture_factory(captures, config.get('PICTURE', 'orientation')) factory.set_background(background) if any(elem != '' for elem in texts): for params in zip(texts, text_fonts, colors, alignments): factory.add_text(*params) if config.getboolean('PICTURE', 'captures_cropping'): factory.set_cropping() if overlay: factory.set_overlay(overlay) picture_file = osp.join(captures_folders, captures_folder + "_pibooth.jpg") factory.save(picture_file)
def state_processing_do(self, cfg, app): idx = app.capture_choices.index(app.capture_nbr) self.texts_vars['date'] = datetime.strptime(app.capture_date, "%Y-%m-%d-%H-%M-%S") self.texts_vars['count'] = app.count LOGGER.info("Saving raw captures") captures = app.camera.get_captures() for savedir in cfg.gettuple('GENERAL', 'directory', 'path'): rawdir = osp.join(savedir, "raw", app.capture_date) os.makedirs(rawdir) for capture in captures: count = captures.index(capture) capture.save(osp.join(rawdir, "pibooth{:03}.jpg".format(count))) LOGGER.info("Creating the final picture") default_factory = get_picture_factory( captures, cfg.get('PICTURE', 'orientation')) factory = self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=default_factory) app.previous_picture = factory.build() for savedir in cfg.gettuple('GENERAL', 'directory', 'path'): app.previous_picture_file = osp.join(savedir, app.picture_filename) factory.save(app.previous_picture_file) if cfg.getboolean('WINDOW', 'animate') and app.capture_nbr > 1: LOGGER.info("Asyncronously generate pictures for animation") for capture in captures: default_factory = get_picture_factory( (capture, ), cfg.get('PICTURE', 'orientation'), force_pil=True, dpi=200) factory = self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=default_factory) factory.set_margin(factory._margin // 3) # 1/3 since DPI is divided by 3 self.factory_pool.add(factory)
def state_processing_do(self, cfg, app): idx = app.capture_choices.index(app.capture_nbr) with timeit("Saving raw captures"): captures = app.camera.get_captures() for savedir in cfg.gettuple('GENERAL', 'directory', 'path'): rawdir = osp.join(savedir, "raw", app.capture_date) os.makedirs(rawdir) for capture in captures: count = captures.index(capture) capture.save( osp.join(rawdir, "pibooth{:03}.jpg".format(count))) with timeit("Creating the final picture"): default_factory = get_picture_factory( captures, cfg.get('PICTURE', 'orientation')) factory = self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=default_factory) app.previous_picture = factory.build() for savedir in cfg.gettuple('GENERAL', 'directory', 'path'): app.previous_picture_file = osp.join(savedir, app.picture_filename) factory.save(app.previous_picture_file) if cfg.getboolean('WINDOW', 'animate') and app.capture_nbr > 1: with timeit("Asyncronously generate pictures for animation"): for capture in captures: default_factory = get_picture_factory( (capture, ), cfg.get('PICTURE', 'orientation'), force_pil=True) factory = self._pm.hook.pibooth_setup_picture_factory( cfg=cfg, opt_index=idx, factory=default_factory) self.factory_pool.add(factory)