File size: 2,529 Bytes
c712a4b
 
 
ac333a5
 
 
 
 
c712a4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac333a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pydicom
import numpy as np
from PIL import Image
import time
from datetime import datetime
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch

def load_dicom(filepath):
    """
    Reads a DICOM file and returns a PIL Image.
    """
    dicom = pydicom.dcmread(filepath)
    
    # Extract the pixel array
    pixel_array = dicom.pixel_array.astype(float)
    
    # Normalize to 0-255 range
    pixel_min = pixel_array.min()
    pixel_max = pixel_array.max()
    normalized = (pixel_array - pixel_min) / (pixel_max - pixel_min) * 255
    
    # Convert to uint8 RGB image
    image = Image.fromarray(normalized.astype(np.uint8)).convert("RGB")
    
    return image

def generate_pdf(report_text):
    """
    Takes report text, returns path to a saved PDF file.
    """
    if not report_text or not report_text.strip():
        return None

    filename = f"brain_mri_report_{int(time.time())}.pdf"
    path = f"/tmp/{filename}"

    c = canvas.Canvas(path, pagesize=letter)
    width, height = letter

    left_margin = 0.75 * inch
    top_margin = height - 0.75 * inch
    line_height = 16
    max_width = width - 2 * left_margin

    # Title
    c.setFont("Helvetica-Bold", 14)
    c.drawString(left_margin, top_margin, "Brain MRI Radiology Report")

    # Date
    c.setFont("Helvetica", 9)
    c.drawString(left_margin, top_margin - 16, f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M')}")

    # Divider line
    c.line(left_margin, top_margin - 24, width - left_margin, top_margin - 24)

    y = top_margin - 0.55 * inch

    # Write report body line by line
    c.setFont("Helvetica", 11)

    for paragraph in report_text.split("\n"):
        words = paragraph.split(" ")
        line = ""

        for word in words:
            test = (line + " " + word).strip()
            if c.stringWidth(test, "Helvetica", 11) <= max_width:
                line = test
            else:
                if y < 0.75 * inch:      # new page if near bottom
                    c.showPage()
                    c.setFont("Helvetica", 11)
                    y = height - 0.75 * inch
                c.drawString(left_margin, y, line)
                y -= line_height
                line = word

        # Draw remaining line
        if y < 0.75 * inch:
            c.showPage()
            c.setFont("Helvetica", 11)
            y = height - 0.75 * inch

        c.drawString(left_margin, y, line)
        y -= line_height

    c.save()
    return path