Back to Open3d

monkey patches visualization and provides helpers to load geometries

docs/jupyter/geometry/file_io.ipynb

0.19.03.1 KB
Original Source
python
import open3d as o3d
import numpy as np
import os
import sys

# monkey patches visualization and provides helpers to load geometries
sys.path.append('..')
import open3d_tutorial as o3dtut
# change to True if you want to interact with the visualization windows
o3dtut.interactive = not "CI" in os.environ

File IO

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

Point cloud

The code below reads and writes a point cloud.

python
print("Testing IO for point cloud ...")
sample_pcd_data = o3d.data.PCDPointCloud()
pcd = o3d.io.read_point_cloud(sample_pcd_data.path)
print(pcd)
o3d.io.write_point_cloud("copy_of_fragment.pcd", pcd)

By default, Open3D tries to infer the file type by the filename extension. The following point cloud file types are supported:

FormatDescription
xyzEach line contains [x, y, z], where x, y, z are the 3D coordinates
xyznEach line contains [x, y, z, nx, ny, nz], where nx, ny, nz are the normals
xyzrgbEach line contains [x, y, z, r, g, b], where r, g, b are in floats of range [0, 1]
ptsThe first line is an integer representing the number of points. The subsequent lines follow one of these formats: [x, y, z, i, r, g, b], [x, y, z, r, g, b], [x, y, z, i] or [x, y, z], where x, y, z, i are of type double and r, g, b are of type uint8
plySee Polygon File Format, the ply file can contain both point cloud and mesh data
pcdSee Point Cloud Data

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

Mesh

The code below reads and writes a mesh.

python
print("Testing IO for meshes ...")
knot_data = o3d.data.KnotMesh() 
mesh = o3d.io.read_triangle_mesh(knot_data.path)
print(mesh)
o3d.io.write_triangle_mesh("copy_of_knot.ply", mesh)

Compared to the point cloud data structure, a mesh has triangles that define the 3D surface.

By default, Open3D tries to infer the file type by the filename extension. The following mesh file types are supported:

FormatDescription
plySee Polygon File Format, the ply file can contain both point cloud and mesh data
stlSee StereoLithography
objSee Object Files
offSee Object File Format
gltf/glbSee GL Transmission Format

Image

The code below reads and writes an image.

python
print("Testing IO for images ...")
image_data = o3d.data.JuneauImage()
img = o3d.io.read_image(image_data.path)
print(img)
o3d.io.write_image("copy_of_Juneau.jpg", img)

The size of the image is readily displayed using print(img).

Both jpg and png image files are supported.