@@ -514,6 +514,10 @@ def write_pcd(self, file_object, undistorted, registered=None):
514514 coloured according to the image otherwise the points are left
515515 uncoloured.
516516
517+ .. note::
518+
519+ Under Python 3 the file object *must* be opened in binary mode.
520+
517521 Args:
518522 file_object (file): A file object to write PCD data to
519523 undistorted (:py:class:`Frame`): the undistorted depth frame
@@ -524,30 +528,31 @@ def write_pcd(self, file_object, undistorted, registered=None):
524528 xs , ys , zs = self .get_points_xyz (undistorted , rows , cols )
525529 n_points = int (np .product (xs .shape ))
526530
527- print ( 'VERSION .7' , file = file_object )
531+ file_object . write ( b 'VERSION .7\n ' )
528532
529533 if registered is None :
530- print ('FIELDS x y z' , file = file_object )
531- print ('SIZE 4 4 4' , file = file_object )
532- print ('TYPE F F F' , file = file_object )
533- print ('COUNT 1 1 1' , file = file_object )
534- data = np .vstack ((- xs .flatten (), - ys .flatten (), - zs .flatten ())).T
534+ file_object .write (
535+ b'FIELDS x y z\n SIZE 4 4 4\n TYPE F F F\n COUNT 1 1 1\n ' )
536+ data = np .zeros ((n_points , 3 ), order = 'C' , dtype = np .float32 )
537+ data [:, 0 ] = - xs .flatten ()
538+ data [:, 1 ] = - ys .flatten ()
539+ data [:, 2 ] = - zs .flatten ()
535540 else :
536- print ('FIELDS x y z rgb' , file = file_object )
537- print ('SIZE 4 4 4 4' , file = file_object )
538- print ('TYPE F F F I' , file = file_object )
539- print ('COUNT 1 1 1 1' , file = file_object )
541+ file_object .write (
542+ b'FIELDS x y z rgb\n SIZE 4 4 4 4\n TYPE F F F F\n COUNT 1 1 1 1\n ' )
540543 bgrx = registered .to_array ().astype (np .uint32 )
541- rgbs = bgrx [..., 0 ] + (bgrx [..., 1 ] << 8 ) + (bgrx [..., 2 ] << 16 )
542- data = np .vstack ((- xs .flatten (), - ys .flatten (), - zs .flatten (), rgbs .flatten ())).T
543-
544- print ('WIDTH {}' .format (undistorted .width ), file = file_object )
545- print ('HEIGHT {}' .format (undistorted .height ), file = file_object )
546- print ('VIEWPOINT 0 0 0 1 0 0 0' , file = file_object )
547- print ('POINTS {}' .format (n_points ), file = file_object )
548- print ('DATA ascii' , file = file_object )
549-
550- assert data .shape [0 ] == n_points
551-
552- for row in data :
553- print (' ' .join ([str (f ) for f in row ]), file = file_object )
544+ rgbs = (
545+ bgrx [..., 0 ] + (bgrx [..., 1 ] << 8 ) + (bgrx [..., 2 ] << 16 )
546+ ).view (np .float32 )
547+ data = np .zeros ((n_points , 4 ), order = 'C' , dtype = np .float32 )
548+ data [:, 0 ] = - xs .flatten ()
549+ data [:, 1 ] = - ys .flatten ()
550+ data [:, 2 ] = - zs .flatten ()
551+ data [:, 3 ] = rgbs .flatten ()
552+
553+ file_object .write ('WIDTH {}\n ' .format (undistorted .width ).encode ())
554+ file_object .write ('HEIGHT {}\n ' .format (undistorted .height ).encode ())
555+ file_object .write (b'VIEWPOINT 0 0 0 1 0 0 0\n ' )
556+ file_object .write ('POINTS {}\n ' .format (n_points ).encode ())
557+ file_object .write (b'DATA binary\n ' )
558+ file_object .write (data .tobytes ())
0 commit comments