Skip to content

Commit 2310f66

Browse files
committed
Replaced image rendering from IPython's image display and overrided PIL's Image.show()
1 parent 1d1620c commit 2310f66

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

bin/run_markdown.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import plotly.graph_objects as go
1212
import sys
1313
import traceback
14+
from PIL import Image
1415

1516

1617
def main():
@@ -83,6 +84,12 @@ def _capture_plotly_show(fig, counter, result, output_dir, stem):
8384
result["html_files"].append(html_filename)
8485
result.setdefault("html_content", []).append(html_content)
8586

87+
def _capture_Image_show(img, counter, result, output_dir, stem):
88+
"""Saves Images instead of displaying them."""
89+
png_filename = f"{stem}_image_{counter}.png"
90+
png_path = output_dir / png_filename
91+
img.save(png_path, "PNG")
92+
result["images"].append(png_filename)
8693

8794
def _generate_markdown(args, content, code_blocks, execution_results, output_dir):
8895
"""Generate the output markdown with embedded results."""
@@ -211,6 +218,7 @@ def _run_all_blocks(args, code_blocks, stem=None, block_number=None):
211218
"__file__": "<markdown_code>",
212219
}
213220
figure_counter = 0
221+
# img_counter = 0
214222
for i, block in enumerate(code_blocks):
215223
if block_number is None:
216224
_report(args.verbose > 1, f"- Executing block {i}/{len(code_blocks)}")
@@ -256,10 +264,18 @@ def patched_show(self, *args, **kwargs):
256264
figure_counter += 1
257265
if stem is not None:
258266
_capture_plotly_show(self, figure_counter, result, output_dir, stem)
267+
def patched_img_show(self, *args, **kwargs):
268+
nonlocal figure_counter
269+
figure_counter += 1
270+
if stem is not None:
271+
_capture_Image_show(self, figure_counter, result, output_dir, stem)
259272
original_show = go.Figure.show
273+
original_img_show = Image.Image.show
274+
Image.Image.show = patched_img_show
260275
go.Figure.show = patched_show
261276
exec(code, exec_globals)
262277
go.Figure.show = original_show
278+
Image.Image.show = original_img_show
263279

264280
except Exception as e:
265281
result["error"] = f"Error executing code: {str(e)}\n{traceback.format_exc()}"

doc/python/static-image-export.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,22 @@ fig = px.bar(data_canada, x='year', y='pop')
200200
img_bytes = fig.to_image(format="png")
201201
```
202202

203-
Here's the bytes object displayed using `IPython.display.Image`:
203+
Here's the bytes object displayed using `Image.show()`:
204204

205205
```python
206-
from IPython.display import Image
207-
Image(img_bytes)
206+
from PIL import Image
207+
from io import BytesIO
208+
img = Image.open(BytesIO(img_bytes))
209+
img.show()
208210
```
209211

210212
## Specify Image Dimensions and Scale
211213
In addition to the image format, the `to_image` and `write_image` functions provide arguments to specify the image `width` and `height` in logical pixels. They also provide a `scale` parameter that can be used to increase (`scale` > 1) or decrease (`scale` < 1) the physical resolution of the resulting image.
212214

213215
```python
214216
img_bytes = fig.to_image(format="png", width=600, height=350, scale=2)
215-
Image(img_bytes)
217+
img = Image.open(BytesIO(img_bytes))
218+
img.show()
216219
```
217220

218221
## Specify Image Export Engine

0 commit comments

Comments
 (0)