Files
wtc.waiswuaschd.de/extract_pdf_images.py
T

36 lines
1.3 KiB
Python

import os
from pypdf import PdfReader
SRC = '/home/maxi/Projekt/wtc.waiswuaschd.de/data/import_2026'
IMG_OUT = '/home/maxi/Projekt/wtc.waiswuaschd.de/app/static/uploads/recipes'
for f in sorted(os.listdir(SRC)):
if not f.lower().endswith('.pdf'):
continue
path = os.path.join(SRC, f)
base = os.path.splitext(f)[0]
safe_name = "".join(c for c in base if c.isalnum() or c in (' ', '_', '-')).strip().replace(' ', '_').lower()
try:
reader = PdfReader(path)
img_idx = 0
best_img = None
best_size = 0
best_ext = '.jpg'
for page in reader.pages:
for img in page.images:
data = img.data
if len(data) > 20000 and len(data) > best_size: # > 20KB
best_size = len(data)
best_img = data
best_ext = os.path.splitext(img.name)[1].lower() or '.jpg'
if best_img:
target_file = f"{safe_name}{best_ext}"
target_path = os.path.join(IMG_OUT, target_file)
with open(target_path, 'wb') as out_f:
out_f.write(best_img)
print(f"Extracted PDF image for {f}: {target_file} ({best_size} bytes)")
except Exception as e:
print(f"Error reading PDF {f}: {e}")