Skip to content

Commit

Permalink
[skip ci] add ti.imdisplay for IPython users
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jun 24, 2020
1 parent f4e7db2 commit a705b36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/taichi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from taichi.misc import *
from taichi.misc.gui import GUI
from taichi.misc.np2ply import PLYWriter
from taichi.misc.image import imread, imwrite, imshow
from taichi.misc.image import *
from taichi.misc.task import Task
from taichi.misc.test import *
from taichi.misc import settings as settings
Expand Down
27 changes: 25 additions & 2 deletions python/taichi/misc/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import taichi as ti


def imwrite(img, filename):
def imcook(img):
if not isinstance(img, np.ndarray):
img = img.to_numpy()

Expand All @@ -23,7 +23,30 @@ def imwrite(img, filename):
comp = img.shape[2]
assert comp in [1, 3, 4], "Image must be either RGB/RGBA or greyscale"

img = np.ascontiguousarray(img.swapaxes(0, 1)[::-1, :])
return img.swapaxes(0, 1)[::-1, :]



def imdisplay(img):
"""
Try to display image in interactive shell.
"""
if ti.lang.shell.oinspect.name.startswith('IPython'):
import PIL.Image
from io import BytesIO
import IPython.display
import numpy as np
img = imcook(img)
f = BytesIO()
PIL.Image.fromarray(img).save(f, 'png')
IPython.display.display(IPython.display.Image(data=f.getvalue()))
else:
ti.imshow(img)


def imwrite(img, filename):
img = imcook(img)
img = np.ascontiguousarray(img)
ptr = img.ctypes.data
ti.core.imwrite(filename, ptr, resx, resy, comp)

Expand Down

0 comments on commit a705b36

Please sign in to comment.