|
1 | | -from typing import Any, io |
| 1 | +from typing import Any |
2 | 2 |
|
3 | 3 | from IPython.core.display_functions import display |
4 | | -from PIL import ImageShow |
5 | 4 | from PIL.Image import Image |
6 | | - |
7 | | -original_show = ImageShow.show |
| 5 | +from PIL.ImageShow import UnixViewer |
8 | 6 |
|
9 | 7 |
|
10 | 8 | def show_file(self, path: str, **options: Any) -> int: |
11 | 9 | # To prevent errors from trying to display image without any display |
12 | 10 | return 0 |
13 | 11 |
|
14 | 12 |
|
15 | | -ImageShow.show_file = show_file |
| 13 | +UnixViewer.show_file = show_file |
16 | 14 |
|
17 | | -original_save = Image.save |
| 15 | +original_show = UnixViewer.show |
18 | 16 |
|
19 | 17 |
|
20 | | -# To prevent circular save and display calls |
21 | | -def __repr_image(self, image_format: str, **kwargs: Any) -> bytes | None: |
22 | | - """Helper function for iPython display hook. |
| 18 | +def show(self, image, **options): |
| 19 | + display(image) |
| 20 | + original_show(self, image, **options) |
23 | 21 |
|
24 | | - :param image_format: Image format. |
25 | | - :returns: image as bytes, saved into the given format. |
26 | | - """ |
27 | | - b = io.BytesIO() |
28 | | - try: |
29 | | - original_save(self, b, image_format, **kwargs) |
30 | | - except Exception: |
31 | | - return None |
32 | | - return b.getvalue() |
33 | 22 |
|
| 23 | +UnixViewer.show = show |
34 | 24 |
|
35 | | -Image._repr_image = __repr_image |
| 25 | +original_save = Image.save |
36 | 26 |
|
37 | 27 |
|
38 | 28 | def save(image, fp, format=None, **options): |
39 | | - display(image) |
| 29 | + if isinstance(fp, str): |
| 30 | + display(image) |
| 31 | + |
40 | 32 | original_save(image, fp, format, **options) |
41 | 33 |
|
42 | 34 |
|
|
0 commit comments