# Help Decoding Gazebo Image Data from JSON Output

**URL:** <https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645>\
**Category:** Gazebo Help\
**Created:** [January 24, 2025, 7:24am UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645 "2025-01-24T07:24:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kakcalu13](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kakcalu13/32/26887_2.png) [@kakcalu13](https://discourse.openrobotics.org/u/kakcalu13)\
**Post date:** [January 24, 2025, 7:24am UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/1 "2025-01-24T07:24:51Z")

</div>

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:

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

```

The output looks like this:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/ros/original/2X/a/a209c82b5dfcf09c4b886479ee06547abb60503c.jpeg)

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

---

<div class="post-metadata">

**Author:** ![peci1](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/peci1/32/10145_2.png) [@peci1](https://discourse.openrobotics.org/u/peci1)\
**Post date:** [January 24, 2025, 9:25am UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/2 "2025-01-24T09:25:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kakcalu13](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kakcalu13/32/26887_2.png) [@kakcalu13](https://discourse.openrobotics.org/u/kakcalu13)\
**Post date:** [January 24, 2025, 12:46pm UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/3 "2025-01-24T12:46:34Z")

</div>

That’s what I did too.

```auto
                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`

---

<div class="post-metadata">

**Author:** ![yjphhw](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/yjphhw/32/32542_2.png) [@yjphhw](https://discourse.openrobotics.org/u/yjphhw)\
**Post date:** [January 27, 2025, 12:25pm UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/4 "2025-01-27T12:25:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![azeey](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/azeey/32/6795_2.png) [@azeey](https://discourse.openrobotics.org/u/azeey)\
**Post date:** [January 27, 2025, 3:26pm UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/5 "2025-01-27T15:26:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kakcalu13](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kakcalu13/32/26887_2.png) [@kakcalu13](https://discourse.openrobotics.org/u/kakcalu13)\
**Post date:** [January 28, 2025, 11:33pm UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/6 "2025-01-28T23:33:50Z")

</div>

ahh that’s QUITE useful! Thank you!!

---

<div class="post-metadata">

**Author:** ![kakcalu13](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kakcalu13/32/26887_2.png) [@kakcalu13](https://discourse.openrobotics.org/u/kakcalu13)\
**Post date:** [January 28, 2025, 11:41pm UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/7 "2025-01-28T23:41:53Z")

</div>

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

Here’s the code for example:

```auto
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())

```

---

<div class="post-metadata">

**Author:** ![yjphhw](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/yjphhw/32/32542_2.png) [@yjphhw](https://discourse.openrobotics.org/u/yjphhw)\
**Post date:** [January 29, 2025, 1:12am UTC](https://discourse.openrobotics.org/t/help-decoding-gazebo-image-data-from-json-output/47645/8 "2025-01-29T01:12:50Z")

</div>

what you show is command line interface.

Gazebo provide python api interface through transport lib

you can look into paget [Gazebo Transport: Python Support](https://gazebosim.org/api/transport/14/python.html) 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](http://qq.com))
