draw_bbox.py 927 B

1234567891011121314151617181920
  1. from magic_pdf.libs import fitz # PyMuPDF
  2. # PDF文件路径
  3. pdf_path = "D:\\project\\20231108code-clean\\code-clean\\tmp\\unittest\\download-pdfs\\scihub\\scihub_53700000\\libgen.scimag53724000-53724999.zip_10.1097\\00129191-200509000-00018.pdf"
  4. doc = fitz.open(pdf_path) # Open the PDF
  5. # 你的数据
  6. data = [[[-2, 0, 603, 80, 24]], [[-3, 0, 602, 80, 24]]]
  7. # 对每个页面进行处理
  8. for i, page in enumerate(doc):
  9. # 获取当前页面的数据
  10. page_data = data[i]
  11. for img in page_data:
  12. x0, y0, x1, y1, _ = img
  13. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  14. page.draw_rect(rect_coords, color=(1, 0, 0), fill=None, width=1.5, overlay=True) # Draw the rectangle
  15. # Save the PDF
  16. doc.save("D:\\project\\20231108code-clean\\code-clean\\tmp\\unittest\\download-pdfs\\scihub\\scihub_53700000\\libgen.scimag53724000-53724999.zip_10.1097\\00129191-200509000-00018_new.pdf")