draw_bbox.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from magic_pdf.libs.commons import fitz # PyMuPDF
  2. from magic_pdf.libs.ocr_content_type import ContentType
  3. def draw_bbox_without_number(i, bbox_list, page, rgb_config, fill_config):
  4. new_rgb = []
  5. for item in rgb_config:
  6. item = float(item) / 255
  7. new_rgb.append(item)
  8. page_data = bbox_list[i]
  9. for bbox in page_data:
  10. x0, y0, x1, y1 = bbox
  11. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  12. if fill_config:
  13. page.draw_rect(rect_coords, color=None, fill=new_rgb, fill_opacity=0.3, width=0.5, overlay=True) # Draw the rectangle
  14. else:
  15. page.draw_rect(rect_coords, color=new_rgb, fill=None, fill_opacity=1, width=0.5, overlay=True) # Draw the rectangle
  16. def draw_bbox_with_number(i, bbox_list, page, rgb_config, fill_config):
  17. new_rgb = []
  18. for item in rgb_config:
  19. item = float(item) / 255
  20. new_rgb.append(item)
  21. page_data = bbox_list[i]
  22. for j, bbox in enumerate(page_data):
  23. x0, y0, x1, y1 = bbox
  24. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  25. if fill_config:
  26. page.draw_rect(rect_coords, color=None, fill=new_rgb, fill_opacity=0.3, width=0.5, overlay=True) # Draw the rectangle
  27. else:
  28. page.draw_rect(rect_coords, color=new_rgb, fill=None, fill_opacity=1, width=0.5, overlay=True) # Draw the rectangle
  29. page.insert_text((x0, y0+10), str(j + 1), fontsize=10, color=new_rgb) # Insert the index at the top left corner of the rectangle
  30. def draw_layout_bbox(pdf_info, pdf_bytes, out_path):
  31. layout_bbox_list = []
  32. blocks_bbox_list = []
  33. dropped_bbox_list = []
  34. for page in pdf_info:
  35. page_layout_list = []
  36. page_dropped_list = []
  37. page_blocks_bbox_list = []
  38. for layout in page['layout_bboxes']:
  39. page_layout_list.append(layout['layout_bbox'])
  40. layout_bbox_list.append(page_layout_list)
  41. for dropped_bbox in page['discarded_blocks']:
  42. page_dropped_list.append(dropped_bbox['bbox'])
  43. dropped_bbox_list.append(page_dropped_list)
  44. for block in page['para_blocks']:
  45. page_blocks_bbox_list.append(block['bbox'])
  46. blocks_bbox_list.append(page_blocks_bbox_list)
  47. pdf_docs = fitz.open("pdf", pdf_bytes)
  48. for i, page in enumerate(pdf_docs):
  49. draw_bbox_with_number(i, layout_bbox_list, page, [255, 0, 0], False)
  50. draw_bbox_without_number(i, dropped_bbox_list, page, [0, 255, 0], True)
  51. draw_bbox_without_number(i, blocks_bbox_list, page, [0, 0, 255], True)
  52. # Save the PDF
  53. pdf_docs.save(f"{out_path}/layout.pdf")
  54. def draw_text_bbox(pdf_info_dict, pdf_bytes, out_path):
  55. text_list = []
  56. inline_equation_list = []
  57. interline_equation_list = []
  58. for page in pdf_info_dict.values():
  59. page_text_list = []
  60. page_inline_equation_list = []
  61. page_interline_equation_list = []
  62. for block in page['para_blocks']:
  63. for line in block['lines']:
  64. for span in line['spans']:
  65. if span['type'] == ContentType.Text:
  66. page_text_list.append(span['bbox'])
  67. elif span['type'] == ContentType.InlineEquation:
  68. page_inline_equation_list.append(span['bbox'])
  69. elif span['type'] == ContentType.InterlineEquation:
  70. page_interline_equation_list.append(span['bbox'])
  71. text_list.append(page_text_list)
  72. inline_equation_list.append(page_inline_equation_list)
  73. interline_equation_list.append(page_interline_equation_list)
  74. pdf_docs = fitz.open("pdf", pdf_bytes)
  75. for i, page in enumerate(pdf_docs):
  76. # 获取当前页面的数据
  77. draw_bbox_without_number(i, text_list, page, [255, 0, 0])
  78. draw_bbox_without_number(i, inline_equation_list, page, [0, 255, 0])
  79. draw_bbox_without_number(i, interline_equation_list, page, [0, 0, 255])
  80. # Save the PDF
  81. pdf_docs.save(f"{out_path}/text.pdf")