Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def concatenate_pictures_opencv(portrait, pictures, footer_texts, bg_color, text_color, footer_fonts, inter_width=None):
""" Merge up to 4 PIL images using opencv to manipulate the images.
"""
with timeit("Create final image with opencv"):
matrix_raw_width, matrix_raw_height, inter_width = get_pics_layout_size(pictures, portrait, inter_width)
final_width, final_height, matrix_width, matrix_height, footer_size = get_final_image_dimensions(
portrait, footer_texts)
offset_generator = get_pics_layout_offset(pictures, portrait, inter_width)
with timeit("Init final image with background"):
pics_scaling_factor = min(matrix_width / matrix_raw_width, matrix_height / matrix_raw_height)
pics_x_offset = int(matrix_width - matrix_raw_width * pics_scaling_factor) // 2
pics_y_offset = int(matrix_height - matrix_raw_height * pics_scaling_factor) // 2
final_image = new_image_with_background_opencv(final_width, final_height, bg_color)
with timeit("Layout pictures matrix"):
# Consider that the photo are correctly ordered
for i in range(len(pictures)):
cv_pic = np.array(pictures[i].convert('RGB'))
cv_pic = cv2.resize(cv_pic, None, fx=pics_scaling_factor,
fy=pics_scaling_factor, interpolation=cv2.INTER_AREA)
(h, w) = cv_pic.shape[:2]
x_offset, y_offset = next(offset_generator)
x_offset, y_offset = pics_x_offset + \
int(pics_scaling_factor * x_offset), pics_y_offset + int(pics_scaling_factor * y_offset)
def do_actions(self, events):
if self.app.find_print_event(events) and self.app.previous_picture_file:
with timeit("Send final picture to printer"):
self.app.led_print.switch_on()
self.app.printer.print_file(self.app.previous_picture_file,
self.app.config.getint('PRINTER', 'pictures_per_page'))
time.sleep(1) # Just to let the LED switched on
self.app.nbr_duplicates += 1
self.app.led_print.blink()
self.printed = True
def entry_actions(self):
with timeit("Show picture choice (nothing selected)"):
self.app.window.set_print_number(0) # Hide printer status
self.app.window.show_choice(self.app.capture_choices)
self.app.capture_nbr = None
self.app.led_capture.blink()
self.app.led_print.blink()
self.timer.start()
def state_print_enter(self, cfg, app, win):
with timeit("Display the final picture"):
win.set_print_number(len(app.printer.get_all_tasks()), not app.printer.is_available())
win.show_print(app.previous_picture)
# Reset timeout in case of settings changed
self.print_view_timer.timeout = cfg.getfloat('PRINTER', 'printer_delay')
self.print_view_timer.start()
final_image = new_image_with_background_opencv(final_width, final_height, bg_color)
with timeit("Layout pictures matrix"):
# Consider that the photo are correctly ordered
for i in range(len(pictures)):
cv_pic = np.array(pictures[i].convert('RGB'))
cv_pic = cv2.resize(cv_pic, None, fx=pics_scaling_factor,
fy=pics_scaling_factor, interpolation=cv2.INTER_AREA)
(h, w) = cv_pic.shape[:2]
x_offset, y_offset = next(offset_generator)
x_offset, y_offset = pics_x_offset + \
int(pics_scaling_factor * x_offset), pics_y_offset + int(pics_scaling_factor * y_offset)
final_image[y_offset:(y_offset + h), x_offset:(x_offset + w)] = cv_pic
# cv2.imshow("final_image", final_image); cv2.waitKey(); cv2.destroyAllWindows()
with timeit("Convert final image from opencv to PIL image"):
final_image = Image.fromarray(final_image)
with timeit("Write text on final image"):
if footer_size:
draw_footer_text(final_image, portrait, footer_texts, footer_fonts, footer_size, text_color)
return final_image
def state_chosen_enter(self, app, win):
with timeit("Show picture choice ({} captures selected)".format(app.capture_nbr)):
win.show_choice(app.capture_choices, selected=app.capture_nbr)
self.layout_timer.start()
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 state_print_do(self, cfg, app, events):
if app.find_capture_event(events):
with timeit("Moving the picture in the forget folder"):
for savedir in cfg.gettuple('GENERAL', 'directory', 'path'):
forgetdir = osp.join(savedir, "forget")
if not osp.isdir(forgetdir):
os.makedirs(forgetdir)
os.rename(osp.join(savedir, app.picture_filename), osp.join(forgetdir, app.picture_filename))
self._reset_vars(app)
app.previous_picture = self.second_previous_picture
# Deactivate the print function for the backuped picture
# as we don't known how many times it has already been printed
app.nbr_duplicates = cfg.getint('PRINTER', 'max_duplicates') + 1
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,
def save(self, path):
"""Build if not already done and save final image in a file.
:param path: path to save
:type path: str
:return: PIL.Image instance
:rtype: object
"""
dirname = osp.dirname(osp.abspath(path))
if not osp.isdir(dirname):
os.mkdir(dirname)
image = self.build()
with timeit("Save image '{}'".format(path)):
image.save(path)
return image