Esempio n. 1
0
def process_photo(update: Update, context: CallbackContext,
                  file_ids: List[str], is_beautify: bool) -> None:
    _ = set_lang(update, context)
    if is_beautify:
        update.effective_message.reply_text(
            _("Beautifying and converting your photos"),
            reply_markup=ReplyKeyboardRemove(),
        )
    else:
        update.effective_message.reply_text(
            _("Converting your photos into PDF"),
            reply_markup=ReplyKeyboardRemove())

    # Setup temporary files
    temp_files = [tempfile.NamedTemporaryFile() for _ in range(len(file_ids))]
    photo_files = []

    # Download all photos
    for i, file_id in enumerate(file_ids):
        file_name = temp_files[i].name
        photo_file = context.bot.get_file(file_id)
        photo_file.download(custom_path=file_name)
        photo_files.append(file_name)

    with tempfile.TemporaryDirectory() as dir_name:
        if is_beautify:
            out_fn = os.path.join(dir_name, "Beautified.pdf")
            noteshrink.notescan_main(photo_files,
                                     basename=f"{dir_name}/page",
                                     pdfname=out_fn)
            send_result_file(update, context, out_fn, "beautify")
        else:
            out_fn = os.path.join(dir_name, "Converted.pdf")
            with open(out_fn, "wb") as f:
                f.write(img2pdf.convert(photo_files))

            send_result_file(update, context, out_fn, "to_pdf")

    # Clean up files
    for tf in temp_files:
        tf.close()
Esempio n. 2
0
def shrink_files(files, basename):
    noteshrink.notescan_main(
        noteshrink.get_argument_parser().parse_args(['-b', basename] + files))
    for file in glob('*.png'):
        if file.startswith(basename):
            return file