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
25
26
27
28
29
30
31
32
33
# examples/Python/Basic/file_io.py

import open3d as o3d

if __name__ == "__main__":

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

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

    print("Testing IO for textured meshes ...")
    textured_mesh = o3d.io.read_triangle_mesh("../../TestData/crate/crate.obj")
    print(textured_mesh)
    o3d.io.write_triangle_mesh("copy_of_crate.obj",
                               textured_mesh,
                               write_triangle_uvs=True)
    copy_textured_mesh = o3d.io.read_triangle_mesh('copy_of_crate.obj')
    print(copy_textured_mesh)

    print("Testing IO for images ...")
    img = o3d.io.read_image("../../TestData/lena_color.jpg")
    print(img)
    o3d.io.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 = o3d.io.read_point_cloud("../../TestData/fragment.pcd")
    print(pcd)
    o3d.io.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 the file type by the filename 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 = o3d.io.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 = o3d.io.read_triangle_mesh("../../TestData/knot.ply")
    print(mesh)
    o3d.io.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.

By default, Open3D tries to infer the file type by the filename extension. Below is a list of supported triangle mesh file types.

Format

Description

ply

See Polygon File Format, the ply file can contain both point cloud and mesh

stl

See StereoLithography

obj

See Object Files

off

See Object File Format

gltf

See GL Transmission Format

Image

This script reads and writes an image.

21
22
23
24
    print("Testing IO for textured meshes ...")
    textured_mesh = o3d.io.read_triangle_mesh("../../TestData/crate/crate.obj")
    print(textured_mesh)
    o3d.io.write_triangle_mesh("copy_of_crate.obj",

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.