File IO

This tutorial shows how basic geometries are read and written by Open3D.

 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# examples/Python/Tutorial/Basic/file_io.py

from open3d import *

if __name__ == "__main__":

    print("Testing IO for point cloud ...")
    pcd = read_point_cloud("../../TestData/fragment.pcd")
    print(pcd)
    write_point_cloud("copy_of_fragment.pcd", pcd)

    print("Testing IO for meshes ...")
    mesh = read_triangle_mesh("../../TestData/knot.ply")
    print(mesh)
    write_triangle_mesh("copy_of_knot.ply", mesh)

    print("Testing IO for images ...")
    img = read_image("../../TestData/lena_color.jpg")
    print(img)
    write_image("copy_of_lena_color.jpg", img)

Point cloud

This script reads and writes a point cloud.

11
12
13
14
    print("Testing IO for point cloud ...")
    pcd = read_point_cloud("../../TestData/fragment.pcd")
    print(pcd)
    write_point_cloud("copy_of_fragment.pcd", pcd)

print() function can be used for displaying a summary of pcd. Output message is below:

Testing IO for point cloud ...
PointCloud with 113662 points.

By default, Open3D tries to infer point cloud file type by extension. Below is a list of supported point cloud file types.

Format Description
xyz Each line contains [x, y, z], where x, y, z are the 3D coordinates
xyzn Each line contains [x, y, z, nx, ny, nz], where nx, ny, nz are the normals
xyzrgb Each line contains [x, y, z, r, g, b], where r, g, b are in floats of range [0, 1]
pts
The first line is an integer representing the number of points
Each subsequent line contains [x, y, z, i, r, g, b], where r, g, b are in `uint8`
ply See Polygon File Format, the ply file can contain both point cloud and mesh
pcd See Point Cloud Data

It’s also possible to specify the file type explicitly. In this case, the file extension will be ignored.

pcd = read_point_cloud("my_points.txt", format='xyz')

Mesh

This script reads and writes a mesh.

16
17
18
19
    print("Testing IO for meshes ...")
    mesh = read_triangle_mesh("../../TestData/knot.ply")
    print(mesh)
    write_triangle_mesh("copy_of_knot.ply", mesh)

Compared to the data structure of point cloud, mesh has triangles that define surface.

Testing IO for meshes ...
TriangleMesh with 1440 points and 2880 triangles.

Image

This script reads and writes an image.

21
22
23
24
    print("Testing IO for images ...")
    img = read_image("../../TestData/lena_color.jpg")
    print(img)
    write_image("copy_of_lena_color.jpg", img)

Size of image is readily displayed using print(img).

Testing IO for images ...
Image of size 512x512, with 3 channels.
Use numpy.asarray to access buffer data.