utils.py 917 B

1234567891011121314151617181920212223242526272829303132
  1. import fitz
  2. import numpy as np
  3. from magic_pdf.utils.annotations import ImportPIL
  4. @ImportPIL
  5. def fitz_doc_to_image(doc, dpi=200) -> dict:
  6. """Convert fitz.Document to image, Then convert the image to numpy array.
  7. Args:
  8. doc (_type_): pymudoc page
  9. dpi (int, optional): reset the dpi of dpi. Defaults to 200.
  10. Returns:
  11. dict: {'img': numpy array, 'width': width, 'height': height }
  12. """
  13. from PIL import Image
  14. mat = fitz.Matrix(dpi / 72, dpi / 72)
  15. pm = doc.get_pixmap(matrix=mat, alpha=False)
  16. # If the width or height exceeds 9000 after scaling, do not scale further.
  17. if pm.width > 9000 or pm.height > 9000:
  18. pm = doc.get_pixmap(matrix=fitz.Matrix(1, 1), alpha=False)
  19. img = Image.frombytes('RGB', (pm.width, pm.height), pm.samples)
  20. img = np.array(img)
  21. img_dict = {'img': img, 'width': pm.width, 'height': pm.height}
  22. return img_dict