Update app.py
Browse files
app.py
CHANGED
|
@@ -188,41 +188,43 @@ def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Im
|
|
| 188 |
pdf = FPDF()
|
| 189 |
pdf.add_page()
|
| 190 |
|
| 191 |
-
#
|
| 192 |
pdf.set_font("helvetica", "B", 16)
|
| 193 |
-
pdf.cell(
|
| 194 |
pdf.ln(10)
|
| 195 |
|
| 196 |
# Patient Info
|
| 197 |
pdf.set_font("helvetica", "", 12)
|
| 198 |
-
pdf.cell(
|
| 199 |
-
pdf.cell(
|
| 200 |
pdf.ln(10)
|
| 201 |
|
| 202 |
# Diagnosis
|
| 203 |
pdf.set_font("helvetica", "B", 14)
|
| 204 |
-
pdf.cell(
|
| 205 |
pdf.set_font("helvetica", "", 12)
|
| 206 |
-
pdf.cell(
|
| 207 |
-
pdf.cell(
|
| 208 |
pdf.ln(10)
|
| 209 |
|
| 210 |
# Lesion Analysis
|
| 211 |
pdf.set_font("helvetica", "B", 14)
|
| 212 |
-
pdf.cell(
|
| 213 |
pdf.set_font("helvetica", "", 12)
|
| 214 |
-
pdf.cell(
|
| 215 |
-
pdf.cell(
|
| 216 |
-
pdf.cell(
|
| 217 |
pdf.ln(15)
|
| 218 |
|
| 219 |
# Helper function to add images
|
| 220 |
def add_pdf_image(pdf, image, title):
|
| 221 |
img_byte_arr = io.BytesIO()
|
| 222 |
image.save(img_byte_arr, format='PNG')
|
|
|
|
|
|
|
| 223 |
pdf.set_font("helvetica", "B", 12)
|
| 224 |
-
pdf.cell(
|
| 225 |
-
pdf.image(io.BytesIO(
|
| 226 |
pdf.ln(5)
|
| 227 |
|
| 228 |
# Add images
|
|
@@ -233,11 +235,10 @@ def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Im
|
|
| 233 |
# Footer
|
| 234 |
pdf.ln(10)
|
| 235 |
pdf.set_font("helvetica", "I", 10)
|
| 236 |
-
pdf.cell(
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
return pdf_output.encode('latin1') if isinstance(pdf_output, str) else pdf_output
|
| 241 |
|
| 242 |
except Exception as e:
|
| 243 |
st.error(f"PDF generation failed: {str(e)}")
|
|
|
|
| 188 |
pdf = FPDF()
|
| 189 |
pdf.add_page()
|
| 190 |
|
| 191 |
+
# Modern FPDF2 syntax
|
| 192 |
pdf.set_font("helvetica", "B", 16)
|
| 193 |
+
pdf.cell(txt="Diabetic Retinopathy Diagnosis Report", new_x="LMARGIN", new_y="NEXT", align='C')
|
| 194 |
pdf.ln(10)
|
| 195 |
|
| 196 |
# Patient Info
|
| 197 |
pdf.set_font("helvetica", "", 12)
|
| 198 |
+
pdf.cell(txt="Patient: ___________________________", new_x="LMARGIN", new_y="NEXT")
|
| 199 |
+
pdf.cell(txt="Date: _____________________________", new_x="LMARGIN", new_y="NEXT")
|
| 200 |
pdf.ln(10)
|
| 201 |
|
| 202 |
# Diagnosis
|
| 203 |
pdf.set_font("helvetica", "B", 14)
|
| 204 |
+
pdf.cell(txt="Diagnosis:", new_x="LMARGIN", new_y="NEXT")
|
| 205 |
pdf.set_font("helvetica", "", 12)
|
| 206 |
+
pdf.cell(txt=f"Stage: {diagnosis}", new_x="LMARGIN", new_y="NEXT")
|
| 207 |
+
pdf.cell(txt=f"Grading: {grade}", new_x="LMARGIN", new_y="NEXT")
|
| 208 |
pdf.ln(10)
|
| 209 |
|
| 210 |
# Lesion Analysis
|
| 211 |
pdf.set_font("helvetica", "B", 14)
|
| 212 |
+
pdf.cell(txt="Lesion Analysis:", new_x="LMARGIN", new_y="NEXT")
|
| 213 |
pdf.set_font("helvetica", "", 12)
|
| 214 |
+
pdf.cell(txt=f"Bright Lesions: {bright_area:.2f}%", new_x="LMARGIN", new_y="NEXT")
|
| 215 |
+
pdf.cell(txt=f"Red Lesions: {red_area:.2f}%", new_x="LMARGIN", new_y="NEXT")
|
| 216 |
+
pdf.cell(txt=f"Total Affected Area: {bright_area + red_area:.2f}%", new_x="LMARGIN", new_y="NEXT")
|
| 217 |
pdf.ln(15)
|
| 218 |
|
| 219 |
# Helper function to add images
|
| 220 |
def add_pdf_image(pdf, image, title):
|
| 221 |
img_byte_arr = io.BytesIO()
|
| 222 |
image.save(img_byte_arr, format='PNG')
|
| 223 |
+
img_bytes = img_byte_arr.getvalue()
|
| 224 |
+
|
| 225 |
pdf.set_font("helvetica", "B", 12)
|
| 226 |
+
pdf.cell(txt=title, new_x="LMARGIN", new_y="NEXT")
|
| 227 |
+
pdf.image(io.BytesIO(img_bytes), x=10, w=180)
|
| 228 |
pdf.ln(5)
|
| 229 |
|
| 230 |
# Add images
|
|
|
|
| 235 |
# Footer
|
| 236 |
pdf.ln(10)
|
| 237 |
pdf.set_font("helvetica", "I", 10)
|
| 238 |
+
pdf.cell(txt="This report was generated by DR Analysis System", new_x="LMARGIN", new_y="NEXT", align='C')
|
| 239 |
|
| 240 |
+
# Get PDF as bytes
|
| 241 |
+
return pdf.output()
|
|
|
|
| 242 |
|
| 243 |
except Exception as e:
|
| 244 |
st.error(f"PDF generation failed: {str(e)}")
|