把 .xyz 文件转换成 .ply 文件

把 .xyz 文件转换成 .ply 文件 (加头文件)_python将.ply转为.xyz-CSDN博客

from plyfile import PlyData,PlyElement
import re
import numpy as np
from os.path import join

def write_ply(save_path,points,text=True):
    
    points = [(points[i,0], points[i,1], points[i,2]) for i in range(points.shape[0])]
    vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4')])
    el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
    PlyData([el], text=text).write(save_path)


A = []
timestamps = []

with open('/Users/manmi/Documents/data/square_data/mm_data/timestamp.txt') as f:
    content = f.readlines()
    for line in content:
        timestamp = int(line)
        timestamps.append(timestamp)
timestamps.sort()      


for k in range(0, len(timestamps)):
    src_tamestamp = timestamps[k]
    path = join('/Users/manmi/Documents/data/square_data/mm_data/mm_xyz', str(src_tamestamp)+'.xyz')              
    with open(path) as f:
        content = f.readlines()
        for line in content:
            x = float(re.split('\s+', line)[0])
            y = float(re.split('\s+', line)[1])
            z = float(re.split('\s+', line)[2])
            A.append((x,y,z))
    points = np.array(A)

    savepath = join('/Users/manmi/Documents/data/square_data/mm_data/mm_ply',str(src_tamestamp)+'.ply')
    write_ply(savepath, points, text=True)

 

posted on 2024-06-15 09:29  一杯明月  阅读(85)  评论(0编辑  收藏  举报