|
10 | 10 | from typing import Literal |
11 | 11 |
|
12 | 12 | import pandas as pd |
13 | | -import plotly |
| 13 | +import plotly.io as pio |
| 14 | +import plotly.graph_objects as go |
14 | 15 | from PIL import Image |
15 | 16 |
|
16 | 17 | from protzilla.utilities import format_trace |
@@ -284,33 +285,47 @@ def __repr__(self): |
284 | 285 | def empty(self) -> bool: |
285 | 286 | return len(self.plots) == 0 |
286 | 287 |
|
287 | | - def export(self, format_): |
| 288 | + def export(self, settings: dict) -> list: |
| 289 | + """ |
| 290 | + Converts all plots from this step to files according to the format and size in the Plotly template. |
| 291 | + An exported plot is represented as BytesIO object containing binary image data. |
| 292 | + :param settings: Dict containing the plot settings. |
| 293 | + :return: List of all exported plots. |
| 294 | + """ |
| 295 | + from ui.settings.plot_template import get_scale_factor |
288 | 296 | exports = [] |
| 297 | + format_ = settings["file_format"] |
| 298 | + |
289 | 299 | for plot in self.plots: |
290 | | - if isinstance(plot, plotly.graph_objs.Figure): |
291 | | - if format_ in ["eps", "tiff"]: |
292 | | - png_binary = plotly.io.to_image(plot, format="png", scale=4) |
293 | | - img = Image.open(BytesIO(png_binary)).convert("RGB") |
| 300 | + scale_factor = get_scale_factor(plot, settings) |
| 301 | + # For Plotly GO Figure |
| 302 | + if isinstance(plot, go.Figure): |
| 303 | + if format_ in ["tiff", "eps"]: |
| 304 | + binary_png = pio.to_image(plot, format="png", scale=scale_factor) |
| 305 | + img = Image.open(BytesIO(binary_png)).convert("RGB") |
294 | 306 | binary = BytesIO() |
295 | 307 | if format_ == "tiff": |
296 | 308 | img.save(binary, format="tiff", compression="tiff_lzw") |
297 | | - else: |
| 309 | + elif format_ == "eps": |
298 | 310 | img.save(binary, format=format_) |
| 311 | + binary.seek(0) |
299 | 312 | exports.append(binary) |
300 | 313 | else: |
301 | | - binary_string = plotly.io.to_image(plot, format=format_, scale=4) |
302 | | - exports.append(BytesIO(binary_string)) |
| 314 | + binary_png = pio.to_image(plot, format=format_, scale=scale_factor) |
| 315 | + exports.append(BytesIO(binary_png)) |
303 | 316 | elif isinstance(plot, dict) and "plot_base64" in plot: |
304 | 317 | plot = plot["plot_base64"] |
305 | 318 |
|
306 | | - if isinstance(plot, bytes): # base64 encoded plots |
307 | | - if format_ in ["eps", "tiff"]: |
| 319 | + # TO DO: Include scale_factor here |
| 320 | + # For base64 encoded plot |
| 321 | + if isinstance(plot, bytes): |
| 322 | + if format_ in ["tiff", "eps"]: |
308 | 323 | img = Image.open(BytesIO(base64.b64decode(plot))).convert("RGB") |
309 | 324 | binary = BytesIO() |
310 | 325 | if format_ == "tiff": |
311 | 326 | img.save(binary, format="tiff", compression="tiff_lzw") |
312 | | - else: |
313 | | - img.save(binary, format=format_) |
| 327 | + elif format_ == "eps": |
| 328 | + img.save(binary, format="eps") |
314 | 329 | binary.seek(0) |
315 | 330 | exports.append(binary) |
316 | 331 | elif format_ in ["png", "jpg"]: |
|
0 commit comments