DSatishchandra commited on
Commit
46557f5
·
verified ·
1 Parent(s): 41d3a05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -37,12 +37,11 @@ def extract_text_from_images(image_paths):
37
 
38
  # Step 3: Rebuild the PDF with Extracted Text
39
  def rebuild_pdf(original_pdf_path, text_data, output_pdf_path):
40
- """Overlay extracted text onto the original PDF."""
41
  with fitz.open(original_pdf_path) as pdf:
42
  output_pdf = fitz.open()
43
 
44
  for page_num, page in enumerate(pdf):
45
- # Render page as an image for reference
46
  pix = page.get_pixmap(dpi=300)
47
  img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
48
 
@@ -53,16 +52,27 @@ def rebuild_pdf(original_pdf_path, text_data, output_pdf_path):
53
  for bbox, text, conf in text_data[page_num]:
54
  if conf > 0.6: # Confidence threshold
55
  (x_min, y_min), (x_max, y_max) = bbox[0], bbox[2]
56
- rect = fitz.Rect(x_min, y_min, x_max, y_max)
 
 
 
 
 
 
57
  new_page.insert_textbox(
58
- rect, text, fontsize=12, fontname="helv", color=(0, 0, 0)
 
 
 
 
 
59
  )
60
 
61
  # Add original diagrams and graphics
62
  new_page.show_pdf_page(page.rect, pdf, page_num)
63
 
64
- # Save rebuilt PDF
65
- output_pdf.save(output_pdf_path)
66
 
67
 
68
  # Full Workflow
@@ -98,4 +108,4 @@ if uploaded_pdf:
98
  output_pdf_path = process_pdf("temp_uploaded.pdf", output_folder)
99
  st.write("Download the processed PDF below:")
100
  with open(output_pdf_path, "rb") as f:
101
- st.download_button("Download PDF", f, file_name="processed_output.pdf")
 
37
 
38
  # Step 3: Rebuild the PDF with Extracted Text
39
  def rebuild_pdf(original_pdf_path, text_data, output_pdf_path):
40
+ """Overlay extracted text onto the original PDF with enhanced alignment and font embedding."""
41
  with fitz.open(original_pdf_path) as pdf:
42
  output_pdf = fitz.open()
43
 
44
  for page_num, page in enumerate(pdf):
 
45
  pix = page.get_pixmap(dpi=300)
46
  img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
47
 
 
52
  for bbox, text, conf in text_data[page_num]:
53
  if conf > 0.6: # Confidence threshold
54
  (x_min, y_min), (x_max, y_max) = bbox[0], bbox[2]
55
+
56
+ # Apply dynamic alignment adjustments
57
+ adjusted_x_min = x_min + 2 # Shift horizontally
58
+ adjusted_y_min = y_min + 3 # Shift vertically
59
+ adjusted_rect = fitz.Rect(adjusted_x_min, adjusted_y_min, x_max, y_max)
60
+
61
+ # Insert text with font embedding
62
  new_page.insert_textbox(
63
+ adjusted_rect, # Use adjusted bounding box
64
+ text,
65
+ fontsize=10, # Adjust font size for better fit
66
+ fontname="helv", # Use Helvetica for better compatibility
67
+ fontfile="path/to/font.ttf", # Embed font (replace with actual font path)
68
+ color=(0, 0, 0), # Black text
69
  )
70
 
71
  # Add original diagrams and graphics
72
  new_page.show_pdf_page(page.rect, pdf, page_num)
73
 
74
+ # Save rebuilt PDF with optimized structure
75
+ output_pdf.save(output_pdf_path, garbage=4)
76
 
77
 
78
  # Full Workflow
 
108
  output_pdf_path = process_pdf("temp_uploaded.pdf", output_folder)
109
  st.write("Download the processed PDF below:")
110
  with open(output_pdf_path, "rb") as f:
111
+ st.download_button("Download PDF", f, file_name="processed_output.pdf")