Skip to content

Commit 46f1b36

Browse files
committed
Fix image generation
1 parent 022d7b6 commit 46f1b36

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

js/tests/images/bar.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ sandboxTest('test show image', async ({ sandbox }) => {
99
from PIL import Image
1010
1111
imarray = numpy.random.rand(16,16,3) * 255
12-
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
12+
image = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
1313
1414
image.show()
15+
print("done")
1516
`
1617

1718
const execution = await sandbox.runCode(code)
@@ -26,7 +27,7 @@ sandboxTest('test image represent', async ({ sandbox }) => {
2627
from PIL import Image
2728
2829
imarray = numpy.random.rand(16,16,3) * 255
29-
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
30+
image = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
3031
3132
image
3233
`
@@ -43,7 +44,7 @@ sandboxTest('test image represent', async ({ sandbox }) => {
4344
from PIL import Image
4445
4546
imarray = numpy.random.rand(16,16,3) * 255
46-
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
47+
image = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
4748
4849
image.save("test.png")
4950
`

python/tests/images/test_images.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ async def test_show_image(async_sandbox: AsyncSandbox):
77
from PIL import Image
88
99
imarray = numpy.random.rand(16,16,3) * 255
10-
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
10+
image = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
1111
1212
image.show()
13+
print("Image shown.")
1314
"""
1415

1516
execution = await async_sandbox.run_code(code)
@@ -24,7 +25,7 @@ async def test_image_as_last_command(async_sandbox: AsyncSandbox):
2425
from PIL import Image
2526
2627
imarray = numpy.random.rand(16,16,3) * 255
27-
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
28+
image = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
2829
2930
image
3031
"""
@@ -40,9 +41,10 @@ async def test_get_image_on_save(async_sandbox: AsyncSandbox):
4041
from PIL import Image
4142
4243
imarray = numpy.random.rand(16,16,3) * 255
43-
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
44+
image = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
4445
4546
image.save("test.png")
47+
print("Image saved.")
4648
"""
4749

4850
execution = await async_sandbox.run_code(code)

template/startup_scripts/0003_images.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
1-
from typing import Any, io
1+
from typing import Any
22

33
from IPython.core.display_functions import display
4-
from PIL import ImageShow
54
from PIL.Image import Image
6-
7-
original_show = ImageShow.show
5+
from PIL.ImageShow import UnixViewer
86

97

108
def show_file(self, path: str, **options: Any) -> int:
119
# To prevent errors from trying to display image without any display
1210
return 0
1311

1412

15-
ImageShow.show_file = show_file
13+
UnixViewer.show_file = show_file
1614

17-
original_save = Image.save
15+
original_show = UnixViewer.show
1816

1917

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)
2321

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()
3322

23+
UnixViewer.show = show
3424

35-
Image._repr_image = __repr_image
25+
original_save = Image.save
3626

3727

3828
def save(image, fp, format=None, **options):
39-
display(image)
29+
if isinstance(fp, str):
30+
display(image)
31+
4032
original_save(image, fp, format, **options)
4133

4234

0 commit comments

Comments
 (0)