cut_image.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. from loguru import logger
  2. from magic_pdf.config.ocr_content_type import ContentType
  3. from magic_pdf.libs.commons import join_path
  4. from magic_pdf.libs.pdf_image_tools import cut_image
  5. def ocr_cut_image_and_table(spans, page, page_id, pdf_bytes_md5, imageWriter):
  6. def return_path(type):
  7. return join_path(pdf_bytes_md5, type)
  8. for span in spans:
  9. span_type = span['type']
  10. if span_type == ContentType.Image:
  11. if not check_img_bbox(span['bbox']) or not imageWriter:
  12. continue
  13. span['image_path'] = cut_image(span['bbox'], page_id, page, return_path=return_path('images'),
  14. imageWriter=imageWriter)
  15. elif span_type == ContentType.Table:
  16. if not check_img_bbox(span['bbox']) or not imageWriter:
  17. continue
  18. span['image_path'] = cut_image(span['bbox'], page_id, page, return_path=return_path('tables'),
  19. imageWriter=imageWriter)
  20. return spans
  21. def check_img_bbox(bbox) -> bool:
  22. if any([bbox[0] >= bbox[2], bbox[1] >= bbox[3]]):
  23. logger.warning(f'image_bboxes: 错误的box, {bbox}')
  24. return False
  25. return True