Update report.py
Browse files
report.py
CHANGED
|
@@ -62,6 +62,7 @@ from reportlab.lib.units import inch
|
|
| 62 |
import matplotlib.pyplot as plt
|
| 63 |
import markdown
|
| 64 |
from xml.etree import ElementTree as ET
|
|
|
|
| 65 |
|
| 66 |
class ReportGenerator:
|
| 67 |
def __init__(self):
|
|
@@ -97,13 +98,15 @@ class ReportGenerator:
|
|
| 97 |
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
| 98 |
|
| 99 |
img_buffer.seek(0)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Calculate width and height to maintain aspect ratio
|
| 103 |
max_width = 6.5 * inch # Maximum width (letter width is 8.5 inches, leaving margins)
|
| 104 |
max_height = 4 * inch # Maximum height
|
| 105 |
|
| 106 |
-
img_width, img_height = img.getSize()
|
| 107 |
aspect = img_width / float(img_height)
|
| 108 |
|
| 109 |
if img_width > max_width:
|
|
@@ -114,8 +117,11 @@ class ReportGenerator:
|
|
| 114 |
img_height = max_height
|
| 115 |
img_width = img_height * aspect
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
elements.append(img)
|
| 121 |
elements.append(Spacer(1, 12))
|
|
|
|
| 62 |
import matplotlib.pyplot as plt
|
| 63 |
import markdown
|
| 64 |
from xml.etree import ElementTree as ET
|
| 65 |
+
from PIL import Image as PILImage
|
| 66 |
|
| 67 |
class ReportGenerator:
|
| 68 |
def __init__(self):
|
|
|
|
| 98 |
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
| 99 |
|
| 100 |
img_buffer.seek(0)
|
| 101 |
+
|
| 102 |
+
# Use PIL to get image dimensions
|
| 103 |
+
with PILImage.open(img_buffer) as img:
|
| 104 |
+
img_width, img_height = img.size
|
| 105 |
|
| 106 |
# Calculate width and height to maintain aspect ratio
|
| 107 |
max_width = 6.5 * inch # Maximum width (letter width is 8.5 inches, leaving margins)
|
| 108 |
max_height = 4 * inch # Maximum height
|
| 109 |
|
|
|
|
| 110 |
aspect = img_width / float(img_height)
|
| 111 |
|
| 112 |
if img_width > max_width:
|
|
|
|
| 117 |
img_height = max_height
|
| 118 |
img_width = img_height * aspect
|
| 119 |
|
| 120 |
+
# Reset buffer position
|
| 121 |
+
img_buffer.seek(0)
|
| 122 |
+
|
| 123 |
+
# Create ReportLab Image with calculated dimensions
|
| 124 |
+
img = Image(img_buffer, width=img_width, height=img_height)
|
| 125 |
|
| 126 |
elements.append(img)
|
| 127 |
elements.append(Spacer(1, 12))
|