Spaces:
Runtime error
Runtime error
Shroominic
commited on
Commit
·
8b66dd3
1
Parent(s):
aa722ee
🪐 fix displaying in jupyter
Browse files
codeinterpreterapi/schema/file.py
CHANGED
|
@@ -52,8 +52,27 @@ class File(BaseModel):
|
|
| 52 |
img_io = BytesIO(self.content)
|
| 53 |
img = Image.open(img_io)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Display the image
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def __str__(self):
|
| 59 |
return self.name
|
|
|
|
| 52 |
img_io = BytesIO(self.content)
|
| 53 |
img = Image.open(img_io)
|
| 54 |
|
| 55 |
+
# Convert image to RGB if it's not
|
| 56 |
+
if img.mode not in ('RGB', 'L'): # L is for greyscale images
|
| 57 |
+
img = img.convert('RGB')
|
| 58 |
+
|
| 59 |
# Display the image
|
| 60 |
+
try:
|
| 61 |
+
# Try to get the IPython shell if available.
|
| 62 |
+
shell = get_ipython().__class__.__name__
|
| 63 |
+
|
| 64 |
+
# If the shell is ZMQInteractiveShell, it means we're in a Jupyter notebook or similar.
|
| 65 |
+
if shell == 'ZMQInteractiveShell':
|
| 66 |
+
from IPython.display import display
|
| 67 |
+
display(img)
|
| 68 |
+
else:
|
| 69 |
+
# We're not in a Jupyter notebook.
|
| 70 |
+
img.show()
|
| 71 |
+
except NameError:
|
| 72 |
+
# We're probably not in an IPython environment, use PIL's show.
|
| 73 |
+
img.show()
|
| 74 |
+
|
| 75 |
+
|
| 76 |
|
| 77 |
def __str__(self):
|
| 78 |
return self.name
|