import os
import sys

try:
    import fitz  # PyMuPDF
except ImportError:
    print("fitz not found, trying to install PyMuPDF...")
    os.system(f"{sys.executable} -m pip install PyMuPDF")
    import fitz

pdf_path = r"C:\Users\Certes\Desktop\guitar_score\output\shintakarajima_perfect.pdf"
out_dir = r"C:\Users\Certes\.gemini\antigravity\brain\5805a1e3-c776-4325-8538-351d54b5e0a0"

try:
    doc = fitz.open(pdf_path)
    md_content = "# PDF Visual Inspection\n\n```carousel\n"
    
    for i in range(min(5, len(doc))): # First 5 pages are enough to see the tangling
        page = doc.load_page(i)
        pix = page.get_pixmap(dpi=150)
        out_file = os.path.join(out_dir, f"pdf_page_{i}.png")
        pix.save(out_file)
        
        if i > 0:
            md_content += "<!-- slide -->\n"
        md_content += f"![Page {i+1}]({out_file})\n"
        
    md_content += "```\n"

    with open(os.path.join(out_dir, "PDF_Inspection.md"), "w", encoding="utf-8") as f:
        f.write(md_content)
        
    print("PDF successfully rendered to images and markdown generated.")

except Exception as e:
    print(f"Error rendering PDF: {e}")
