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 |
---|---|
|
Each line contains |
|
Each line contains |
|
Each line contains |
|
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` |
|
See Polygon File Format,
the |
|
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 |
---|---|
|
See Polygon File Format,
the |
|
|
|
See Object Files |
|
|
|
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.