Help Decoding Gazebo Image Data from JSON Output

Hey everyone,

I decided to create a Python library for Gazebo that works natively without relying on ROS2. While I know ROS2 can run on macOS or Windows, I find it too much of a headache to set up on different computers. Since Gazebo is cross-platform, I wanted to develop a library that works independently and make it publicly available, regardless of the SDF/world files being used.

Now, back to the real topic: What format is the ‘data’ compressed into? I can’t figure out which encoding it uses. Here’s the command I ran:

gz topic -e -t /Camera0/image --json-output

The output looks like this:

I’m trying to convert this data from JSON into an ndarray. Does anyone know what this format is or how to decode it?

Could it be base64? JSON itself cannot hold binary data, so I’d assume something like this.

That’s what I did too.

                json_data = json.loads(output)

                test = json_data['data']
                decoded_data = base64.b64decode(test)
                np_array = np.frombuffer(decoded_data, dtype=np.uint8)
                image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
                if image is not None:
                    print(f"Image shape: {image.shape}")
                    cv2.imshow("Decoded Image", image)
                    cv2.waitKey(0)
                    cv2.destroyAllWindows()

this failed. I wonder how did ros2 gazebo work? Is there anywhere that I can look into source?

The output prints NONE

actually, gazebo has transport library, it provides topic ,service and node which are similar to corresponding ROS 2 concept. you can check transport library.

if you want to be tutored, you can send me message.

The JSON output comes directly from Protobuf, so you will probably want to look at https://protobuf.dev/programming-guides/json/. From a quick look, what’s shown in the screenshot is the raw image bytes with base64 encoding.

1 Like

ahh that’s QUITE useful! Thank you!!

Aren’t I already using it? I’m working with topics at the moment.

Here’s the code for example:

def initalize_gyro():
    topic_command = ["gz", "topic", "-e", "-t", "/imu", "--json-output"]
    topic_process = subprocess.Popen(topic_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    return topic_process


def read_gyro(gyro_instance):
    return json.loads(gyro_instance.stdout.readline())

what you show is command line interface.

Gazebo provide python api interface through transport lib

you can look into paget Gazebo Transport: Python Support find how to make subscriber to get topic through programming.

I think the above information is sufficient for you to get image data through Python API of transport lib.

if you want the direct solution and you can spend some fee, you can send me email(yjphhw at qq.com)