# Revision history [back]

### Create a bag out of 16 bit images

Hi,

I want to store a mono16 depth image to a bag The instructions here work for bit depth of 8 bits per pixel (either for mono or rgb8) but not for mono16 or 32FC1.

I'm getting an error from bag.py

field data[] exceeds specified width [uint8]


Where the problem occurs, the field_val is 1574 and the maxval is 256

How can I serialize a 16 bit image into bytes, and write it into the bag?

Thanks, Avner

### Create a bag out of 16 bit images

Hi,

I want to store a mono16 depth image to a bag The instructions here work for bit depth of 8 bits per pixel (either for mono or rgb8) but not for mono16 or 32FC1.

I'm getting an error from bag.py

field data[] exceeds specified width [uint8]


Where the problem occurs, the field_val is 1574 and the maxval is 256

How can I serialize a 16 bit image into bytes, and write it into the bag?

Thanks, Avner

EDIT1

Thanks for your answer @PeteBlackerThe3rd. With your answer here and the answer here I was able to use the example code as a base with the following changes:

diff grabdepth.orig.py grabdepth.new.py
>
> from PIL import Image as Image1
---
>
47,48c50,57
<         np.save("./dframe"+str(i)+".npy",numpy_image)
<         cv2.imwrite("./dframe"+str(i)+".jpg", image_normal)
---
>         np.save("/tmp/tmp1/dframe"+str(i)+".npy",numpy_image)
>
>         # ok - workaround for bug in Pillow (https://github.com/python-pillow/Pillow/issues/2970) for uint16 (via uint32)
>         ar32 = numpy_image.astype(np.uint32)
>         im1 = Image1.fromarray(ar32) # or more verbose as Image.fromarray(ar32, 'I')
>
>         im1.save("/tmp/tmp1/dframe"+str(i)+".png")
---
>
58,59c67,68
<         rospy.Subscriber("/camera/depth_registered/image_raw",Image,checkdepth)
<
---
>         rospy.Subscriber("/camera/camera/depth/image",Image,checkdepth)


With this, I successfully extracted 16 bit depth images from the bag into 16 bits-per-pixel image files.