rnmee commited on
Commit
93bb1b5
·
verified ·
1 Parent(s): 474c097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
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
- # Use built-in font that supports basic characters
192
  pdf.set_font("helvetica", "B", 16)
193
- pdf.cell(0, 10, "Diabetic Retinopathy Diagnosis Report", 0, 1, 'C')
194
  pdf.ln(10)
195
 
196
  # Patient Info
197
  pdf.set_font("helvetica", "", 12)
198
- pdf.cell(0, 10, "Patient: ___________________________", 0, 1)
199
- pdf.cell(0, 10, "Date: _____________________________", 0, 1)
200
  pdf.ln(10)
201
 
202
  # Diagnosis
203
  pdf.set_font("helvetica", "B", 14)
204
- pdf.cell(0, 10, "Diagnosis:", 0, 1)
205
  pdf.set_font("helvetica", "", 12)
206
- pdf.cell(0, 10, f"Stage: {diagnosis}", 0, 1)
207
- pdf.cell(0, 10, f"UK Grading: {grade}", 0, 1)
208
  pdf.ln(10)
209
 
210
  # Lesion Analysis
211
  pdf.set_font("helvetica", "B", 14)
212
- pdf.cell(0, 10, "Lesion Analysis:", 0, 1)
213
  pdf.set_font("helvetica", "", 12)
214
- pdf.cell(0, 10, f"Bright Lesions: {bright_area:.2f}%", 0, 1)
215
- pdf.cell(0, 10, f"Red Lesions: {red_area:.2f}%", 0, 1)
216
- pdf.cell(0, 10, f"Total Affected Area: {bright_area + red_area:.2f}%", 0, 1)
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(0, 10, title, 0, 1)
225
- pdf.image(io.BytesIO(img_byte_arr.getvalue()), x=10, w=180)
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(0, 10, "This report was generated by DR Analysis System", 0, 1, 'C')
237
 
238
- # Get PDF bytes without encoding
239
- pdf_output = pdf.output(dest='S')
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)}")