SathvikGanta commited on
Commit
a7ef481
·
verified ·
1 Parent(s): c485789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -23
app.py CHANGED
@@ -57,31 +57,21 @@ def compare_visual_changes(orig_img, edit_img):
57
 
58
  return overlay
59
 
60
- # Generate visual comparison report with configurable dimensions
61
- def generate_visual_report(images, output_path, page_width=210, page_height=297):
62
- """
63
- Generate a PDF report with visual changes.
64
- :param images: List of images to include in the report.
65
- :param output_path: Path to save the output PDF.
66
- :param page_width: Width of the PDF page in millimeters (default is A4: 210mm).
67
- :param page_height: Height of the PDF page in millimeters (default is A4: 297mm).
68
- """
69
- pdf = FPDF(format=(page_width, page_height)) # Set custom page size
70
  for img in images:
71
  temp_path = "temp_image.png"
72
  cv2.imwrite(temp_path, img)
73
  pdf.add_page()
74
- # Adjust image placement and scaling to fit the custom dimensions
75
- img_width = min(page_width - 20, page_width) # Allow small margins
76
- img_height = img.shape[0] * (img_width / img.shape[1]) # Preserve aspect ratio
77
- pdf.image(temp_path, x=10, y=10, w=img_width, h=min(img_height, page_height - 20))
78
  os.remove(temp_path)
79
 
80
  pdf.output(output_path)
81
  return output_path
82
 
83
  # Perform only visual comparison
84
- def generate_visual_comparison(original_pdf, edited_pdf, page_width=210, page_height=297):
85
  original_images = convert_pdf_to_images(original_pdf)
86
  edited_images = convert_pdf_to_images(edited_pdf)
87
 
@@ -91,16 +81,16 @@ def generate_visual_comparison(original_pdf, edited_pdf, page_width=210, page_he
91
  highlighted_img = compare_visual_changes(orig_img, aligned_img)
92
  visual_combined_images.append(np.hstack((orig_img, highlighted_img)))
93
 
94
- # Generate visual changes report with custom dimensions
95
  visual_report_path = generate_visual_report(
96
- visual_combined_images, "outputs/visual_changes.pdf", page_width, page_height
97
  )
98
 
99
  return visual_report_path
100
 
101
  # Gradio interface function
102
- def pdf_visual_comparison(original_pdf, edited_pdf, page_width, page_height):
103
- visual_path = generate_visual_comparison(original_pdf.name, edited_pdf.name, page_width, page_height)
104
  return visual_path
105
 
106
  # Gradio interface
@@ -108,15 +98,13 @@ interface = gr.Interface(
108
  fn=pdf_visual_comparison,
109
  inputs=[
110
  gr.File(label="Upload Original PDF", file_types=[".pdf"]),
111
- gr.File(label="Upload Edited PDF", file_types=[".pdf"]),
112
- gr.Number(label="Page Width (mm)", value=210),
113
- gr.Number(label="Page Height (mm)", value=297),
114
  ],
115
  outputs=[
116
  gr.File(label="Download Visual Changes Report")
117
  ],
118
  title="PDF Visual Comparison Tool",
119
- description="Upload two PDFs: the original and the edited version. Specify PDF dimensions and generate a visual changes report."
120
  )
121
 
122
  if __name__ == "__main__":
 
57
 
58
  return overlay
59
 
60
+ # Generate visual comparison report
61
+ def generate_visual_report(images, output_path):
62
+ pdf = FPDF()
 
 
 
 
 
 
 
63
  for img in images:
64
  temp_path = "temp_image.png"
65
  cv2.imwrite(temp_path, img)
66
  pdf.add_page()
67
+ pdf.image(temp_path, x=10, y=10, w=190)
 
 
 
68
  os.remove(temp_path)
69
 
70
  pdf.output(output_path)
71
  return output_path
72
 
73
  # Perform only visual comparison
74
+ def generate_visual_comparison(original_pdf, edited_pdf):
75
  original_images = convert_pdf_to_images(original_pdf)
76
  edited_images = convert_pdf_to_images(edited_pdf)
77
 
 
81
  highlighted_img = compare_visual_changes(orig_img, aligned_img)
82
  visual_combined_images.append(np.hstack((orig_img, highlighted_img)))
83
 
84
+ # Generate visual changes report
85
  visual_report_path = generate_visual_report(
86
+ visual_combined_images, "outputs/visual_changes.pdf"
87
  )
88
 
89
  return visual_report_path
90
 
91
  # Gradio interface function
92
+ def pdf_visual_comparison(original_pdf, edited_pdf):
93
+ visual_path = generate_visual_comparison(original_pdf.name, edited_pdf.name)
94
  return visual_path
95
 
96
  # Gradio interface
 
98
  fn=pdf_visual_comparison,
99
  inputs=[
100
  gr.File(label="Upload Original PDF", file_types=[".pdf"]),
101
+ gr.File(label="Upload Edited PDF", file_types=[".pdf"])
 
 
102
  ],
103
  outputs=[
104
  gr.File(label="Download Visual Changes Report")
105
  ],
106
  title="PDF Visual Comparison Tool",
107
+ description="Upload two PDFs: the original and the edited version. The tool generates a visual changes report."
108
  )
109
 
110
  if __name__ == "__main__":