跳转至主要内容

taichi.tools#

Taichi utility module.

  • image submodule for image io.

  • video submodule for exporting results to video files.

  • diagnose submodule for printing system environment information.

class taichi.tools.PLYWriter(num_vertices: int, num_faces=0, face_type='tri', comment='created by PLYWriter')#

Writes numpy.array data to ply files.

Parameters:
  • num_vertices (int) – number of vertices.

  • num_faces (int, optional) – number of faces.

  • face_type (str) – tri or quad.

  • comment (str) – comment message.

add_face_channel(self, key: str, data_type: str, data: numpy.array)#
add_face_id(self)#
add_face_piece(self, piece: numpy.array)#
add_faces(self, indices: numpy.array)#
add_vertex_alpha(self, alpha: numpy.array)#

Sets the alpha-channel (transparent) of the vertex colors.

Parameters:

alpha (numpy.array(float)) – the alpha-channel (transparent) of the colors.

add_vertex_channel(self, key: str, data_type: str, data: numpy.array)#
add_vertex_color(self, r: numpy.array, g: numpy.array, b: numpy.array)#

Sets the (r, g, b) channels of the colors at the vertices.

The three arguments are all numpy arrays of float type and have the same length.

Parameters:
  • r (numpy.array(float)) – the r-channel (red) of the colors.

  • g (numpy.array(float)) – the g-channel (green) of the color.

  • b (numpy.array(float)) – the b-channel (blue) of the colors.

add_vertex_id(self)#

Sets the ids of the vertices.

The id of a vertex is equal to its index in the vertex array.

add_vertex_normal(self, nx: numpy.array, ny: numpy.array, nz: numpy.array)#

Add normal vectors at the vertices.

The three arguments are all numpy arrays of float type and have the same length.

Parameters:
  • nx (numpy.array(float)) – x-coordinates of the normal vectors.

  • ny (numpy.array(float)) – y-coordinates of the normal vectors.

  • nz (numpy.array(float)) – z-coordinates of the normal vectors.

add_vertex_piece(self, piece: numpy.array)#
add_vertex_pos(self, x: numpy.array, y: numpy.array, z: numpy.array)#

Set the (x, y, z) coordinates of the vertices.

Parameters:
  • x (numpy.array(float)) – x-coordinates of the vertices.

  • y (numpy.array(float)) – y-coordinates of the vertices.

  • z (numpy.array(float)) – z-coordinates of the vertices.

add_vertex_rgba(self, r: numpy.array, g: numpy.array, b: numpy.array, a: numpy.array)#

Sets the (r, g, b, a) channels of the colors at the vertices.

Parameters:
  • r (numpy.array(float)) – the r-channel (red) of the colors.

  • g (numpy.array(float)) – the g-channel (green) of the color.

  • b (numpy.array(float)) – the b-channel (blue) of the colors.

  • a (numpy.array(float)) – the a-channel (alpha) of the colors.

add_vertex_vel(self, vx: numpy.array, vy: numpy.array, vz: numpy.array)#

Add velocity vectors at the vertices.

Parameters:
  • vx (numpy.array(float)) – x-coordinates of the velocity vectors.

  • vy (numpy.array(float)) – y-coordinates of the velocity vectors.

  • vz (numpy.array(float)) – z-coordinates of the velocity vectors.

export(self, path)#
export_ascii(self, path)#
export_frame(self, series_num: int, path: str)#
export_frame_ascii(self, series_num: int, path: str)#
print_header(self, path: str, _format: str)#
sanity_check(self)#
class taichi.tools.VideoManager(output_dir, video_filename=None, width=None, height=None, post_processor=None, framerate=24, automatic_build=True)#

Utility class for exporting results to mp4 and gif formats. This class relies on ffmpeg.

Parameters:
  • output_dir (str) – directory to save the frames.

  • video_filename (str) – filename for the video. default filename is video.mp4.

  • width (int) – resolution of the video.

  • height (int) – resolution of the video.

  • post_processor (object) – any object that implements the process(img) method, which accepts an image as a numpy.ndarray and returns the process image.

  • framerate (int) – frame rate of the video.

  • automatic_build (bool) – automatically generate the resulting video or not.

Example:

>>> video_manager = ti.tools.VideoManager(output_dir="./output", framerate=24, automatic_build=False)
>>> for i in range(50):
>>>     render()
>>>     img = pixels.to_numpy()
>>>     video_manager.write_frame(img)
>>>
>>> video_manager.make_video(gif=True, mp4=True)
Returns:

An instance of taichi.tools.VideoManager class.

clean_frames(self)#

Delete all previous image files in the saved directory.

get_frame_directory(self)#

Returns path to the directory where the image files are located in.

get_output_filename(self, suffix)#
make_video(self, mp4=True, gif=True)#

Convert the image files to a mp4 or gif animation.

write_frame(self, img)#

Write an numpy.ndarray img to an image file.

The filename will be automatically determined by this manager and the frame counter.

write_frames(self, images)#

Write a list of numpy.ndarray images to image files.

taichi.tools.imread(filename, channels=0)#

Load image from a specific file.

Parameters:
  • filename (str) – An image filename to load from.

  • channels (int, optional) – The channels hint of input image, Default to 0.

Returns:

An output image loaded from given filename.

Return type:

np.ndarray

taichi.tools.imresize(img, w, h=None)#

Resize an image to a specific size.

Parameters:
  • img (Union[ti.field, np.ndarray]) – A field of of array with shape (width, height, …)

  • w (int) – The output width after resize.

  • h (int, optional) – The output height after resize, will be the same as width if not set. Default to None.

Returns:

An output image after resize input.

Return type:

np.ndarray

taichi.tools.imshow(img, title='imshow')#

Display a taichi.field or a numpy.ndarray in a Taichi GUI window or an interactive Ipython notebook. For an interactive Ipython environment, the image will be shown in the notebook.

Parameters:
  • img (Union[ti.field, np.ndarray]) – A field of of array with shape (width, height) or (height, width, 3) or (height, width, 4).

  • title (str, optional) – The title of GUI window. Default to imshow.

taichi.tools.imwrite(img, filename)#

Save a field to a a specific file.

Parameters:
  • img (Union[ti.field, np.ndarray]) – A field of shape (height, width) or (height, width, 3) or (height, width, 4), if dtype is float-type (ti.f16, ti.f32, np.float32 etc), the value of each pixel should be float between [0.0, 1.0]. Otherwise ti.tools.imwrite will first clip them into [0.0, 1.0] if dtype is int-type (ti.u8, ti.u16, np.uint8 etc), , the value of each pixel can be any valid integer in its own bounds. These integers in this field will be scaled to [0, 255] by being divided over the upper bound of its basic type accordingly.

  • filename (str) – The filename to save to.