Rosanswers logo

I want to save image from a bag file.And I have fellow the example, but I will always get a error like this:


Traceback (most recent call last): 
File "image.py", line 55, in main() File "image.py", line 50, in main readImage(topic_name) File "image.py", line 31, in readImage cv_image = CvBridge().imgmsg_to_cv2(msg, "32FC1") File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 163, in imgmsg_to_cv2 dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding) AttributeError: '_sensor_msgs__CameraInfo' object has no attribute 'encoding'

Ros version:

ROS Kinetic Kame

And the cv_bridge is in the path:


I print out the attr of the "msg"

['__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_check_types', '_connection_header', '_full_text', '_get_types', '_has_header', '_md5sum', '_slot_types', '_spec', '_type', 'data', 'deserialize', 'deserialize_numpy', 'encoding', 'header', 'height', 'is_bigendian', 'serialize', 'serialize_numpy', 'step', 'width']

By the way I get this error too:

Unrecognized image encoding [yuyv]

my code:

def readImage(topic_name):  
    bag  = rosbag.Bag('2018-11-20-15-54-07.bag') 

    bridge = CvBridge()

    for topic, msg, t in bag.read_messages(topics=topic_name):
            print dir(msg)
            cv_image = CvBridge().imgmsg_to_cv2(msg, "32FC1")
        timestr = "%.6f" %  msg.header.stamp.to_sec()
            image_name = timestr+ ".jpg"
        cv2.imwrite(image_name, cv_image)
        except CvBridgeError as e:
            print e

Any ideas ! thanks a lot

Originally posted by daohu527 on ROS Answers with karma: 16 on 2019-01-19

Post score: 0

Original comments

Comment by jayess on 2019-01-19:
I don't know if you noticed, but that tutorial is for Diamondback which is very old and is no longer supported.

Comment by ahendrix on 2019-01-19:
It kinds looks like you're trying to convert CameraInfo messages (you should be trying to convert Image messages). Maybe double-check that you're reading from the correct topic.

Comment by PeteBlackerThe3rd on 2019-01-20:
Your code also appears to be trying to read single channel 32 bit floating point images from the bag and save them as JPEGs. JPEG doesn't support float images so that will fail, also as @ahendrix mentioned your trying to read a camera_info topic which carries calibration info not images.


1 Answer 1


Rosanswers logo

@ahendrix You are right, I had convert the "image_short" and "image_short_info", so I remove the "image_short_info", and the error is disappear. @PeteBlackerThe3rd As you mentioned I have remove the camera_info topic,and I got the error "Unrecognized image encoding [yuyv]" Thx!

Originally posted by daohu527 with karma: 16 on 2019-01-20

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.