pdf_to_images.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import fitz # PyMuPDF
  2. import sys
  3. import os
  4. import json
  5. def convert_pdf_to_images(pdf_path, output_dir, zoom=2.0, quality=85):
  6. """
  7. Converts PDF pages to images.
  8. zoom: 2.0 means 200% scaling (approx 144 DPI if original is 72 DPI)
  9. """
  10. try:
  11. if not os.path.exists(output_dir):
  12. os.makedirs(output_dir)
  13. doc = fitz.open(pdf_path)
  14. images = []
  15. # Matrix for scaling (DPI control)
  16. mat = fitz.Matrix(zoom, zoom)
  17. for i in range(len(doc)):
  18. page = doc.load_page(i)
  19. pix = page.get_pixmap(matrix=mat, colorspace=fitz.csRGB)
  20. output_path = os.path.join(output_dir, f"page-{i+1}.jpg")
  21. # In newer PyMuPDF, save() doesn't take quality. Use tobytes instead.
  22. img_bytes = pix.tobytes("jpg", jpg_quality=quality)
  23. with open(output_path, "wb") as f:
  24. f.write(img_bytes)
  25. images.append({
  26. "path": output_path,
  27. "pageIndex": i + 1,
  28. "size": os.path.getsize(output_path)
  29. })
  30. doc.close()
  31. return {
  32. "success": True,
  33. "images": images,
  34. "totalPages": len(images)
  35. }
  36. except Exception as e:
  37. return {
  38. "success": False,
  39. "error": str(e)
  40. }
  41. if __name__ == "__main__":
  42. if len(sys.argv) < 3:
  43. print(json.dumps({"success": False, "error": "Usage: python pdf_to_images.py <pdf_path> <output_dir> [zoom] [quality]"}))
  44. sys.exit(1)
  45. pdf_path = sys.argv[1]
  46. output_dir = sys.argv[2]
  47. zoom = float(sys.argv[3]) if len(sys.argv) > 3 else 2.0
  48. quality = int(sys.argv[4]) if len(sys.argv) > 4 else 85
  49. result = convert_pdf_to_images(pdf_path, output_dir, zoom, quality)
  50. print(json.dumps(result))