Marthee commited on
Commit
7e538e1
·
verified ·
1 Parent(s): a426313

Update Code_2_7.py

Browse files
Files changed (1) hide show
  1. Code_2_7.py +62 -20
Code_2_7.py CHANGED
@@ -1187,7 +1187,8 @@ def normalize_color(color):
1187
  return tuple(min(max(round(c * 255), 0), 255) for c in color)
1188
 
1189
 
1190
-
 
1191
 
1192
  def adjustannotations(OutputPdfStage1,text_with_positions):
1193
  input_pdf_path = OutputPdfStage1
@@ -1239,7 +1240,7 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1239
  # Normalize and match the color
1240
  annot_color = normalize_color(obj["/C"])
1241
  matched_entry = next(
1242
- ((text, NBS) for text,NBS, _, color in text_with_positions if annot_color == color),
1243
  (None, None)
1244
  )
1245
  # print("matched_entry = ",matched_entry)
@@ -1300,30 +1301,53 @@ def remove_duplicate_annotations(pdf_path, threshold):
1300
  to_delete = set()
1301
 
1302
  # Extract annotation positions and colors
1303
- for annot_index, annot_ref in enumerate(annotations):
1304
- annot = annot_ref.get_object()
1305
 
1306
- if "/Rect" in annot and "/C" in annot:
1307
- rect = annot["/Rect"]
1308
- if isinstance(rect, ArrayObject): # Ensure rect is a list
1309
- rect = list(rect)
1310
 
1311
- color = normalize_color(annot["/C"])
1312
- annots_data.append((annot_index, rect, color))
 
 
 
 
 
 
 
 
 
 
1313
 
1314
- # Compare distances and mark duplicates
1315
  for i, (idx1, rect1, color1) in enumerate(annots_data):
1316
  if idx1 in to_delete:
1317
  continue
1318
- for j, (idx2, rect2, color2) in enumerate(annots_data[i+1:], start=i+1):
 
1319
  if idx2 in to_delete:
1320
  continue
1321
- if color1 == color2 and distance(rect1, rect2) < threshold:
1322
- to_delete.add(idx2) # Mark second annotation for deletion
1323
 
1324
- # Remove duplicates
1325
- new_annotations = [annotations[i] for i in range(len(annotations)) if i not in to_delete]
1326
- page[NameObject("/Annots")] = ArrayObject(new_annotations)
 
 
 
 
 
 
 
 
 
 
 
 
 
1327
 
1328
  writer.add_page(page)
1329
 
@@ -1603,12 +1627,26 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpa
1603
  # Draw the line annotation for distance1
1604
  chosen_start = start_point1
1605
  chosen_end = end_point1
1606
- annot12 = page2.add_line_annot(chosen_start, chosen_end)
 
 
 
 
 
1607
  elif max_distance == distance2:
1608
  # Draw the line annotation for distance2
1609
  chosen_start = start_point2
1610
  chosen_end = end_point2
1611
- annot12 = page2.add_line_annot(chosen_start, chosen_end)
 
 
 
 
 
 
 
 
 
1612
  elif max_distance == half1_distance:
1613
  # annot12 = page2.add_polyline_annot(half1)
1614
  max_pair_distance = 0.0
@@ -1633,7 +1671,11 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpa
1633
  # the two consecutive points with the greatest separation.
1634
  if max_pair_start is not None and max_pair_end is not None:
1635
  # 6. Draw the line annotation using these two points
1636
- annot12 = page2.add_line_annot(max_pair_start, max_pair_end)
 
 
 
 
1637
  # print(f"Drew line annotation between {max_pair_start} and {max_pair_end}")
1638
  else:
1639
  # This case only occurs if half1 has fewer than 2 points
 
1187
  return tuple(min(max(round(c * 255), 0), 255) for c in color)
1188
 
1189
 
1190
+ def color_close_enough(c1, c2, threshold=10):
1191
+ return all(abs(a - b) <= threshold for a, b in zip(c1, c2))
1192
 
1193
  def adjustannotations(OutputPdfStage1,text_with_positions):
1194
  input_pdf_path = OutputPdfStage1
 
1240
  # Normalize and match the color
1241
  annot_color = normalize_color(obj["/C"])
1242
  matched_entry = next(
1243
+ ((text, NBS) for text,NBS, _, color in text_with_positions if color_close_enough(annot_color, color)),
1244
  (None, None)
1245
  )
1246
  # print("matched_entry = ",matched_entry)
 
1301
  to_delete = set()
1302
 
1303
  # Extract annotation positions and colors
1304
+ # for annot_index, annot_ref in enumerate(annotations):
1305
+ # annot = annot_ref.get_object()
1306
 
1307
+ # if "/Rect" in annot and "/C" in annot:
1308
+ # rect = annot["/Rect"]
1309
+ # if isinstance(rect, ArrayObject): # Ensure rect is a list
1310
+ # rect = list(rect)
1311
 
1312
+ # color = normalize_color(annot["/C"])
1313
+ # annots_data.append((annot_index, rect, color))
1314
+
1315
+ for i, annot_ref in enumerate(annotations):
1316
+ annot = annot_ref.get_object()
1317
+ rect = annot.get("/Rect")
1318
+ color = annot.get("/C")
1319
+
1320
+ if rect and color and isinstance(rect, ArrayObject) and len(rect) == 4:
1321
+ norm_color = normalize_color(color)
1322
+ annots_data.append((i, list(rect), norm_color))
1323
+
1324
 
 
1325
  for i, (idx1, rect1, color1) in enumerate(annots_data):
1326
  if idx1 in to_delete:
1327
  continue
1328
+ for j in range(i + 1, len(annots_data)):
1329
+ idx2, rect2, color2 = annots_data[j]
1330
  if idx2 in to_delete:
1331
  continue
1332
+ if color_close_enough(color1, color2) and distance(rect1, rect2) < threshold:
1333
+ to_delete.add(idx2)
1334
 
1335
+ # Keep only non-duplicates
1336
+ new_annots = [annotations[i] for i in range(len(annotations)) if i not in to_delete]
1337
+ page[NameObject("/Annots")] = ArrayObject(new_annots)
1338
+ # Compare distances and mark duplicates
1339
+ # for i, (idx1, rect1, color1) in enumerate(annots_data):
1340
+ # if idx1 in to_delete:
1341
+ # continue
1342
+ # for j, (idx2, rect2, color2) in enumerate(annots_data[i+1:], start=i+1):
1343
+ # if idx2 in to_delete:
1344
+ # continue
1345
+ # if color1 == color2 and distance(rect1, rect2) < threshold:
1346
+ # to_delete.add(idx2) # Mark second annotation for deletion
1347
+
1348
+ # # Remove duplicates
1349
+ # new_annotations = [annotations[i] for i in range(len(annotations)) if i not in to_delete]
1350
+ # page[NameObject("/Annots")] = ArrayObject(new_annotations)
1351
 
1352
  writer.add_page(page)
1353
 
 
1627
  # Draw the line annotation for distance1
1628
  chosen_start = start_point1
1629
  chosen_end = end_point1
1630
+ # annot12 = page2.add_line_annot(chosen_start, chosen_end)
1631
+ points=[]
1632
+ points.append(chosen_start)
1633
+ points.append(chosen_end)
1634
+ annot12 = page2.add_polyline_annot(points)
1635
+
1636
  elif max_distance == distance2:
1637
  # Draw the line annotation for distance2
1638
  chosen_start = start_point2
1639
  chosen_end = end_point2
1640
+ # annot12 = page2.add_line_annot(chosen_start, chosen_end)
1641
+ points=[]
1642
+ points.append(chosen_start)
1643
+ points.append(chosen_end)
1644
+ # annot12 = page2.add_polyline_annot(points)
1645
+ points=[]
1646
+ points.append(chosen_start)
1647
+ points.append(chosen_end)
1648
+ annot12 = page2.add_polyline_annot(points)
1649
+
1650
  elif max_distance == half1_distance:
1651
  # annot12 = page2.add_polyline_annot(half1)
1652
  max_pair_distance = 0.0
 
1671
  # the two consecutive points with the greatest separation.
1672
  if max_pair_start is not None and max_pair_end is not None:
1673
  # 6. Draw the line annotation using these two points
1674
+ # annot12 = page2.add_line_annot(max_pair_start, max_pair_end)
1675
+ points=[]
1676
+ points.append(max_pair_start)
1677
+ points.append(max_pair_end)
1678
+ annot12 = page2.add_polyline_annot(points)
1679
  # print(f"Drew line annotation between {max_pair_start} and {max_pair_end}")
1680
  else:
1681
  # This case only occurs if half1 has fewer than 2 points