diff --git a/deploy.sh b/deploy.sh index 858b694..a1eafb2 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,6 +1,6 @@ #!/bin/bash -total_commands=28 +total_commands=29 current_command=0 function print_progress { @@ -92,6 +92,9 @@ print_progress "cp ./conjurer/latex_commands.py ./Conjurer/" cp ./conjurer/latex_functions.py ./Conjurer/ print_progress "cp ./conjurer/latex_functions.py ./Conjurer/" +cp ./conjurer/librarian_functions.py ./Conjurer +print_progress "cp ./conjurer/librarian_functions.py ./Conjurer/" + cp ./conjurer/thin_client.py ./Conjurer/bot.py print_progress "cp ./conjurer/thin_client.py ./Conjurer/bot.py" diff --git a/librarian_commands.py b/librarian_commands.py index a8342e1..9ebea27 100644 --- a/librarian_commands.py +++ b/librarian_commands.py @@ -8,7 +8,7 @@ from queue import Empty import discord import pdf2image -#import fitz +import fitz import PyPDF2 import requests from discord.ext import commands, tasks diff --git a/librarian_functions.py b/librarian_functions.py new file mode 100644 index 0000000..4855426 --- /dev/null +++ b/librarian_functions.py @@ -0,0 +1,28 @@ +# pdf_render.py +import io + +try: + import fitz # PyMuPDF + _HAS_PYMUPDF = True +except Exception: + _HAS_PYMUPDF = False + from pdf2image import convert_from_path + +def pdf_page_to_image_bytes(path: str, page_index: int = 0, zoom: float = 2.0, fmt: str = "PNG") -> bytes: + """ + Zwraca bytes obrazka z jednej strony PDF: + - PyMuPDF (szybki) jeśli dostępny, + - inaczej pdf2image + poppler (wymaga 'pdftoppm'). + page_index: 0-based + """ + if _HAS_PYMUPDF: + doc = fitz.open(path) + page = doc.load_page(page_index) + mat = fitz.Matrix(zoom, zoom) + pix = page.get_pixmap(matrix=mat, alpha=False) + return pix.tobytes(fmt.lower()) + # fallback + images = convert_from_path(path, first_page=page_index+1, last_page=page_index+1, fmt=fmt) + bio = io.BytesIO() + images[0].save(bio, fmt) + return bio.getvalue() diff --git a/requirements_bot.txt b/requirements_bot.txt index 14d162d..621be26 100644 --- a/requirements_bot.txt +++ b/requirements_bot.txt @@ -13,7 +13,7 @@ spotipy tiktoken PyNaCl flask[async] -fitz +PyMuPDF waitress clickupython assemblyai[extras]