Verificação

parent e3de1613
...@@ -4,14 +4,24 @@ import fitz # PyMuPDF ...@@ -4,14 +4,24 @@ import fitz # PyMuPDF
import os import os
import zipfile import zipfile
from PIL import Image from PIL import Image
import sqlite3
app = Flask(__name__) app = Flask(__name__)
UPLOAD_FOLDER = 'uploads' UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True) os.makedirs(UPLOAD_FOLDER, exist_ok=True)
def init_db():
conn = sqlite3.connect('database.db')
conn.execute('''CREATE TABLE IF NOT EXISTS configuracoes
(id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, coords TEXT)''')
conn.close()
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html') conn = sqlite3.connect('database.db')
configs = conn.execute('SELECT * FROM configuracoes').fetchall()
conn.close()
return render_template('index.html', configs=configs)
@app.route('/upload_preview', methods=['POST']) @app.route('/upload_preview', methods=['POST'])
def upload_preview(): def upload_preview():
...@@ -28,8 +38,10 @@ def preview(filename, pagnumber): ...@@ -28,8 +38,10 @@ def preview(filename, pagnumber):
if not os.path.exists(pdf_path): return "Erro", 404 if not os.path.exists(pdf_path): return "Erro", 404
doc = fitz.open(pdf_path) doc = fitz.open(pdf_path)
# Garante que a página solicitada existe no PDF
total = len(doc) total = len(doc)
pagnumber = max(0, min(pagnumber, total - 1)) if pagnumber >= total: pagnumber = total - 1
if pagnumber < 0: pagnumber = 0
page = doc[pagnumber] page = doc[pagnumber]
pix = page.get_pixmap(dpi=100) pix = page.get_pixmap(dpi=100)
...@@ -42,13 +54,7 @@ def processar(): ...@@ -42,13 +54,7 @@ def processar():
filename = request.form['filename'] filename = request.form['filename']
inicio = int(request.form['inicio']) inicio = int(request.form['inicio'])
fim = int(request.form['fim']) fim = int(request.form['fim'])
c = list(map(float, request.form['coords'].split(',')))
# x, y, w, h, dispW, dispH, realW, realH
data = list(map(float, request.form['coords'].split(',')))
x, y, w, h, dispW, dispH, realW, realH = data
scale_x = realW / dispW
scale_y = realH / dispH
pdf_path = os.path.join(UPLOAD_FOLDER, filename) pdf_path = os.path.join(UPLOAD_FOLDER, filename)
doc = fitz.open(pdf_path) doc = fitz.open(pdf_path)
...@@ -59,16 +65,13 @@ def processar(): ...@@ -59,16 +65,13 @@ def processar():
if p >= len(doc): break if p >= len(doc): break
page = doc[p] page = doc[p]
pix = page.get_pixmap(dpi=300) pix = page.get_pixmap(dpi=300)
img_full = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
dpi_m = 300 / 100 s = 300 / 100 # Escala Preview vs Corte Final
left = x * scale_x * dpi_m area = (c[0]*s, c[1]*s, (c[0]+c[2])*s, (c[1]+c[3])*s)
top = y * scale_y * dpi_m
right = (x + w) * scale_x * dpi_m
bottom = (y + h) * scale_y * dpi_m
img_cortada = img_full.crop((left, top, right, bottom)) img_cortada = img.crop(area)
img_name = f"pagina_{p+1}.png" img_name = f"pagina_{p}.png"
img_path = os.path.join(UPLOAD_FOLDER, img_name) img_path = os.path.join(UPLOAD_FOLDER, img_name)
img_cortada.save(img_path) img_cortada.save(img_path)
zip_f.write(img_path, img_name) zip_f.write(img_path, img_name)
...@@ -78,4 +81,6 @@ def processar(): ...@@ -78,4 +81,6 @@ def processar():
return send_file(zip_path, as_attachment=True) return send_file(zip_path, as_attachment=True)
if __name__ == '__main__': if __name__ == '__main__':
init_db()
app.run(debug=True) app.run(debug=True)
#mt fodinha kjk#
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment