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) maker = get_picture_maker((picture,) * copies) maker.set_margin(2) maker.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 print_file(self, filename, copies=1): """Send a file to the CUPS server to the default printer. """ if not self._notif_server.is_running(): self._notif_server.start() 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 copies > 1: with tempfile.NamedTemporaryFile( suffix=osp.basename(filename)) as fp: picture = Image.open(filename) maker = get_picture_maker((picture, ) * copies) maker.set_margin(2) maker.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 do_actions(self, events): with timeit("Creating the final picture"): captures = self.app.camera.get_captures() backgrounds = self.app.config.gettuple('PICTURE', 'backgrounds', ('color', 'path'), 2) if self.app.capture_nbr == self.app.capture_choices[0]: background = backgrounds[0] else: background = backgrounds[1] overlays = self.app.config.gettuple('PICTURE', 'overlays', 'path', 2) if self.app.capture_nbr == self.app.capture_choices[0]: overlay = overlays[0] else: overlay = overlays[1] texts = [self.app.config.get('PICTURE', 'footer_text1').strip('"'), self.app.config.get('PICTURE', 'footer_text2').strip('"')] colors = self.app.config.gettuple('PICTURE', 'text_colors', 'color', len(texts)) text_fonts = self.app.config.gettuple('PICTURE', 'text_fonts', str, len(texts)) alignments = self.app.config.gettuple('PICTURE', 'text_alignments', str, len(texts)) def _setup_maker(m): m.set_background(background) if any(elem != '' for elem in texts): for params in zip(texts, text_fonts, colors, alignments): m.add_text(*params) if self.app.config.getboolean('PICTURE', 'captures_cropping'): m.set_cropping() if overlay: m.set_overlay(overlay) if self.app.config.getboolean('GENERAL', 'debug'): m.set_outlines() maker = get_picture_maker(captures, self.app.config.get('PICTURE', 'orientation')) _setup_maker(maker) self.app.previous_picture = maker.build() self.app.previous_picture_file = osp.join(self.app.savedir, osp.basename(self.app.dirname) + "_pibooth.jpg") maker.save(self.app.previous_picture_file) if self.app.config.getboolean('WINDOW', 'animate') and self.app.capture_nbr > 1: with timeit("Asyncronously generate pictures for animation"): for capture in captures: maker = get_picture_maker((capture,), self.app.config.get('PICTURE', 'orientation'), force_pil=True) _setup_maker(maker) self.app.makers_pool.add(maker)
def regenerate_all_images(config): captures_folders = config.getpath('GENERAL', 'directory') capture_choices = config.gettuple('PICTURE', 'captures', int) # 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) captures = get_captures(captures_folder_path) LOGGER.info("Generating image from raws in folder %s" % (captures_folder_path)) backgrounds = config.gettuple('PICTURE', 'backgrounds', ('color', 'path'), 2) if len(captures) == capture_choices[0]: background = backgrounds[0] elif len(captures) == capture_choices[1]: background = backgrounds[1] else: LOGGER.warning( "Folder %s doesn't contain the correct number of pictures" % captures_folder_path) continue overlays = config.gettuple('PICTURE', 'overlays', 'path', 2) if len(captures) == capture_choices[0]: overlay = overlays[0] else: overlay = overlays[1] 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)) def _setup_maker(m): m.set_background(background) if any(elem != '' for elem in texts): for params in zip(texts, text_fonts, colors, alignments): m.add_text(*params) if config.getboolean('PICTURE', 'captures_cropping'): m.set_cropping() if overlay: m.set_overlay(overlay) if config.getboolean('GENERAL', 'debug'): m.set_outlines() maker = get_picture_maker(captures, config.get('PICTURE', 'orientation'), force_pil=True) _setup_maker(maker) previous_picture_file = osp.join(captures_folders, captures_folder + "_pibooth.jpg") maker.save(previous_picture_file)
def do_actions(self, events): with timeit("Creating the final picture"): captures = self.app.camera.get_captures() backgrounds = self.app.config.gettuple('PICTURE', 'backgrounds', ('color', 'path'), 2) if self.app.capture_nbr == self.app.capture_choices[0]: background = backgrounds[0] else: background = backgrounds[1] overlays = self.app.config.gettuple('PICTURE', 'overlays', 'path', 2) if self.app.capture_nbr == self.app.capture_choices[0]: overlay = overlays[0] else: overlay = overlays[1] texts = [ self.app.config.get('PICTURE', 'footer_text1').strip('"'), self.app.config.get('PICTURE', 'footer_text2').strip('"') ] colors = self.app.config.gettuple('PICTURE', 'text_colors', 'color', len(texts)) text_fonts = self.app.config.gettuple('PICTURE', 'text_fonts', str, len(texts)) alignments = self.app.config.gettuple('PICTURE', 'text_alignments', str, len(texts)) def _setup_maker(m): m.set_background(background) if any(elem != '' for elem in texts): for params in zip(texts, text_fonts, colors, alignments): m.add_text(*params) if self.app.config.getboolean('PICTURE', 'captures_cropping'): m.set_cropping() if overlay: m.set_overlay(overlay) if self.app.config.getboolean('GENERAL', 'debug'): m.set_outlines() maker = get_picture_maker( captures, self.app.config.get('PICTURE', 'orientation')) _setup_maker(maker) self.app.previous_picture = maker.build() self.app.previous_picture_file = osp.join( self.app.savedir, osp.basename(self.app.dirname) + "_pibooth.jpg") maker.save(self.app.previous_picture_file) # Generate image id pic_crypt_name = gen_hash_filename(self.app.previous_picture_file) # Write exif informations in image file write_exif(self.app.previous_picture_file, self.app.capture_nbr, pic_crypt_name) # Upload picture to server LOGGER.info("Uploading picture") shutil.copyfile(self.app.previous_picture_file, "./pictures/" + pic_crypt_name) generate_qr_code("www.prinsenhof.de/~fotobox/" + pic_crypt_name, "link.jpg", "./pictures/") ftp_upload("./pictures/" + pic_crypt_name, "", "", "", "") os.remove("./pictures/" + pic_crypt_name) if self.app.config.getboolean('WINDOW', 'animate') and self.app.capture_nbr > 1: with timeit("Asyncronously generate pictures for animation"): for capture in captures: maker = get_picture_maker( (capture, ), self.app.config.get('PICTURE', 'orientation'), force_pil=True) _setup_maker(maker) self.app.makers_pool.add(maker)
def do_actions(self, events): with timeit("Creating the final picture"): with timeit("loading images"): captures = self.app.camera.get_captures() with timeit("Creating backgrounds"): backgrounds = self.app.config.gettuple('PICTURE', 'backgrounds', ('color', 'path'), 2) if self.app.capture_nbr == self.app.capture_choices[0]: background = backgrounds[0] else: background = backgrounds[1] with timeit("Creating overlays"): overlays = self.app.config.gettuple('PICTURE', 'overlays', 'path', 2) if self.app.capture_nbr == self.app.capture_choices[0]: overlay = overlays[0] else: overlay = overlays[1] texts = [ self.app.config.get('PICTURE', 'footer_text1').strip('"'), self.app.config.get('PICTURE', 'footer_text2').strip('"') ] colors = self.app.config.gettuple('PICTURE', 'text_colors', 'color', len(texts)) text_fonts = self.app.config.gettuple('PICTURE', 'text_fonts', str, len(texts)) alignments = self.app.config.gettuple('PICTURE', 'text_alignments', str, len(texts)) def _setup_maker(m): m.set_background(background) if any(elem != '' for elem in texts): for params in zip(texts, text_fonts, colors, alignments): m.add_text(*params) if self.app.config.getboolean('PICTURE', 'captures_cropping'): m.set_cropping() if overlay: m.set_overlay(overlay) if self.app.config.getboolean('GENERAL', 'debug'): m.set_outlines() with timeit("make it"): maker = get_picture_maker( captures, self.app.config.get('PICTURE', 'orientation')) _setup_maker(maker) self.app.previous_picture = maker.build() with timeit("Saving the final picture"): self.app.previous_picture_file = osp.join( self.app.savedir, osp.basename(self.app.dirname) + "_pibooth.jpg") maker.save(self.app.previous_picture_file) # Generate image id pic_crypt_name = gen_hash_filename(self.app.previous_picture_file) # Write exif informations in image file write_exif(self.app.previous_picture_file, self.app.capture_nbr, pic_crypt_name, self.app.config) # Upload picture to server LOGGER.info("Uploading picture") if self.app.config.getboolean('SERVER', 'upload_image'): url = self.app.config.get('SERVER', 'server_url').strip('"') pwd = self.app.config.get('SERVER', 'server_pwd').strip('"') #web_upload(file=self.app.previous_picture_file, crypt_name=pic_crypt_name, url=url, pwd=pwd, app=self.app) webfile = osp.join(self.app.webdir, pic_crypt_name + ".jpg") shutil.copy(self.app.previous_picture_file, webfile) self.app.web_upload_sucessful = True qr_code_link = self.app.config.get('SERVER', 'qr_code_link').strip('"') qr_code_link = qr_code_link.replace("{hash}", pic_crypt_name) self.app.previous_picture_qr_file_inverted = osp.join( self.app.tempdir, osp.basename(self.app.dirname) + "_qr_link_inverted.jpg") generate_qr_code(qr_code_link, self.app.previous_picture_qr_file_inverted, inverted=True) self.app.previous_picture_qr_file = osp.join( self.app.tempdir, osp.basename(self.app.dirname) + "_qr_link.jpg") generate_qr_code(qr_code_link, self.app.previous_picture_qr_file, inverted=False) # to print qr code on file: self.app.previous_picture_qr_file_print = osp.join( self.app.tempdir, osp.basename(self.app.dirname) + "_pibooth_qr.jpg") # add_qr_code_to_image(self.app.previous_picture_qr_file, self.app.previous_picture_file, self.app.previous_picture_qr_file_print) texts = [ self.app.config.get('PICTURE', 'footer_text1').strip('"'), self.app.config.get('PICTURE', 'footer_text2').strip('"') ] create_qr_code_print(self.app.previous_picture_qr_file, self.app.previous_picture_qr_file_print, texts, pic_crypt_name) # self.app.previous_picture = add_qr_code_to_image(self.app.previous_picture_qr_file, self.app.previous_picture_file, self.app.previous_picture_file_qith_qr) # self.app.qr_printer.print_file(self.app.previous_picture_qr_file_print, 1) if self.app.config.getboolean('PRINTER', 'black_white'): self.app.previous_print_picture_file = osp.join( self.app.tempdir, osp.basename(self.app.dirname) + "_pibooth_print.jpg") black_white_image(self.app.previous_picture_file, self.app.previous_print_picture_file) else: self.app.previous_print_picture_file = self.app.previous_picture_file if self.app.config.getboolean('WINDOW', 'animate') and self.app.capture_nbr > 1: with timeit("Asyncronously generate pictures for animation"): for capture in captures: maker = get_picture_maker( (capture, ), self.app.config.get('PICTURE', 'orientation'), force_pil=True) _setup_maker(maker) self.app.makers_pool.add(maker)