Convert ROS Image to CompressedImage(png)

asked 2018-04-10 23:00:52 -0500

R gravatar image

I am trying to convert a rosbag with raw images to another rosbag containing compressed images using cv2 bridge:

def compress_to_png(msg):
    bridge = CvBridge()
    img = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
    ret_val, image_buffer = cv2.imencode(".png", img)

    compressed_msg = CompressedImage()
    compressed_msg.header = msg.header
    compressed_msg.data = image_buffer.tostring()
    compressed_msg.format = "png"

    return compressed_msg

However, the new rosbag seems to be having issues when I play it with rqt_bag. More specifically, it throws the below message:

"Can't convert image: image has wrong mode"

This method however works when I convert to jpeg. Only png conversion seems to be running into issues. Any thoughts ?

edit retag flag offensive close merge delete