File tree Expand file tree Collapse file tree 3 files changed +149
-0
lines changed Expand file tree Collapse file tree 3 files changed +149
-0
lines changed Original file line number Diff line number Diff line change 1+ import  {  expect  }  from  'vitest' 
2+ 
3+ import  {  sandboxTest  }  from  '../setup' 
4+ 
5+ // Skip this test if we are running in debug mode — the pwd and user in the testing docker container are not the same as in the actual sandbox. 
6+ sandboxTest ( 'test show image' ,  async  ( {  sandbox } )  =>  { 
7+   const  code  =  ` 
8+     import numpy 
9+     from PIL import Image 
10+      
11+     imarray = numpy.random.rand(16,16,3) * 255 
12+     im = Image.fromarray(imarray.astype('uint8')).convert('RGBA') 
13+      
14+     image.show() 
15+     ` 
16+ 
17+   const  execution  =  await  sandbox . runCode ( code ) 
18+ 
19+   const  image  =  execution . results [ 0 ] . png 
20+   expect ( image ) . toBeDefined ( ) 
21+ } ) 
22+ 
23+ sandboxTest ( 'test image represent' ,  async  ( {  sandbox } )  =>  { 
24+   const  code  =  ` 
25+     import numpy 
26+     from PIL import Image 
27+ 
28+     imarray = numpy.random.rand(16,16,3) * 255 
29+     im = Image.fromarray(imarray.astype('uint8')).convert('RGBA') 
30+ 
31+     image 
32+     ` 
33+   const  execution  =  await  sandbox . runCode ( code ) 
34+ 
35+   const  image  =  execution . results [ 0 ] . png 
36+   expect ( image ) . toBeDefined ( ) 
37+ } ) 
38+ 
39+ 
40+  sandboxTest ( 'get image on save' ,  async  ( {  sandbox } )  =>  { 
41+    const  code  =  ` 
42+     import numpy 
43+     from PIL import Image 
44+ 
45+     imarray = numpy.random.rand(16,16,3) * 255 
46+     im = Image.fromarray(imarray.astype('uint8')).convert('RGBA') 
47+ 
48+     image.save("test.png") 
49+     ` 
50+ 
51+    const  execution  =  await  sandbox . runCode ( code ) 
52+ 
53+    const  image  =  execution . results [ 0 ] . png 
54+    expect ( image ) . toBeDefined ( ) 
55+  } ) 
Original file line number Diff line number Diff line change 1+ from  e2b_code_interpreter .code_interpreter_async  import  AsyncSandbox 
2+ 
3+ 
4+ async  def  test_show_image (async_sandbox : AsyncSandbox ):
5+     code  =  """ 
6+     import numpy 
7+     from PIL import Image 
8+      
9+     imarray = numpy.random.rand(16,16,3) * 255 
10+     im = Image.fromarray(imarray.astype('uint8')).convert('RGBA') 
11+      
12+     image.show() 
13+     """ 
14+ 
15+     execution  =  await  async_sandbox .run_code (code )
16+ 
17+     image  =  execution .results [0 ].png 
18+     assert  image 
19+ 
20+ 
21+ async  def  test_image_as_last_command (async_sandbox : AsyncSandbox ):
22+     code  =  """ 
23+     import numpy 
24+     from PIL import Image 
25+      
26+     imarray = numpy.random.rand(16,16,3) * 255 
27+     im = Image.fromarray(imarray.astype('uint8')).convert('RGBA') 
28+      
29+     image 
30+     """ 
31+     execution  =  await  async_sandbox .run_code (code )
32+ 
33+     image  =  execution .results [0 ].png 
34+     assert  image 
35+ 
36+ 
37+ async  def  test_get_image_on_save (async_sandbox : AsyncSandbox ):
38+     code  =  """ 
39+     import numpy 
40+     from PIL import Image 
41+      
42+     imarray = numpy.random.rand(16,16,3) * 255 
43+     im = Image.fromarray(imarray.astype('uint8')).convert('RGBA') 
44+      
45+     image.save("test.png") 
46+     """ 
47+ 
48+     execution  =  await  async_sandbox .run_code (code )
49+ 
50+     image  =  execution .results [0 ].png 
51+     assert  image 
Original file line number Diff line number Diff line change 1+ from  typing  import  Any , io 
2+ 
3+ from  IPython .core .display_functions  import  display 
4+ from  PIL  import  ImageShow 
5+ from  PIL .Image  import  Image 
6+ 
7+ original_show  =  ImageShow .show 
8+ 
9+ 
10+ def  show_file (self , path : str , ** options : Any ) ->  int :
11+     # To prevent errors from trying to display image without any display 
12+     return  0 
13+ 
14+ 
15+ ImageShow .show_file  =  show_file 
16+ 
17+ original_save  =  Image .save 
18+ 
19+ 
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. 
23+ 
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+ 
34+ 
35+ Image ._repr_image  =  __repr_image 
36+ 
37+ 
38+ def  save (image , fp , format = None , ** options ):
39+     display (image )
40+     original_save (image , fp , format , ** options )
41+ 
42+ 
43+ Image .save  =  save 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments