pdf_filter.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from libs.commons import fitz
  2. from libs.boxbase import _is_in, _is_in_or_part_overlap
  3. from libs.drop_reason import DropReason
  4. def __area(box):
  5. return (box[2] - box[0]) * (box[3] - box[1])
  6. def __is_contain_color_background_rect(page:fitz.Page, text_blocks, image_bboxes) -> bool:
  7. """
  8. 检查page是包含有颜色背景的矩形
  9. """
  10. color_bg_rect = []
  11. p_width, p_height = page.rect.width, page.rect.height
  12. # 先找到最大的带背景矩形
  13. blocks = page.get_cdrawings()
  14. for block in blocks:
  15. if 'fill' in block and block['fill']: # 过滤掉透明的
  16. fill = list(block['fill'])
  17. fill[0], fill[1], fill[2] = int(fill[0]), int(fill[1]), int(fill[2])
  18. if fill==(1.0,1.0,1.0):
  19. continue
  20. rect = block['rect']
  21. # 过滤掉特别小的矩形
  22. if __area(rect) < 10*10:
  23. continue
  24. # 为了防止是svg图片上的色块,这里过滤掉这类
  25. if any([_is_in_or_part_overlap(rect, img_bbox) for img_bbox in image_bboxes]):
  26. continue
  27. color_bg_rect.append(rect)
  28. # 找到最大的背景矩形
  29. if len(color_bg_rect) > 0:
  30. max_rect = max(color_bg_rect, key=lambda x:__area(x))
  31. max_rect_int = (int(max_rect[0]), int(max_rect[1]), int(max_rect[2]), int(max_rect[3]))
  32. # 判断最大的背景矩形是否包含超过3行文字,或者50个字 TODO
  33. if max_rect[2]-max_rect[0] > 0.2*p_width and max_rect[3]-max_rect[1] > 0.1*p_height:#宽度符合
  34. #看是否有文本块落入到这个矩形中
  35. for text_block in text_blocks:
  36. box = text_block['bbox']
  37. box_int = (int(box[0]), int(box[1]), int(box[2]), int(box[3]))
  38. if _is_in(box_int, max_rect_int):
  39. return True
  40. return False
  41. def __is_table_overlap_text_block(text_blocks, table_bbox):
  42. """
  43. 检查table_bbox是否覆盖了text_blocks里的文本块
  44. TODO
  45. """
  46. for text_block in text_blocks:
  47. box = text_block['bbox']
  48. if _is_in_or_part_overlap(table_bbox, box):
  49. return True
  50. return False
  51. def pdf_filter(page:fitz.Page, text_blocks, table_bboxes, image_bboxes) -> tuple:
  52. """
  53. return:(True|False, err_msg)
  54. True, 如果pdf符合要求
  55. False, 如果pdf不符合要求
  56. """
  57. if __is_contain_color_background_rect(page, text_blocks, image_bboxes):
  58. return False, {"need_drop": True, "drop_reason": DropReason.COLOR_BACKGROUND_TEXT_BOX}
  59. return True, None