Skip to content

Instantly share code, notes, and snippets.

@wngreene
Last active October 4, 2024 17:52
Show Gist options
  • Save wngreene/835cda68ddd9c5416defce876a4d7dd9 to your computer and use it in GitHub Desktop.
Save wngreene/835cda68ddd9c5416defce876a4d7dd9 to your computer and use it in GitHub Desktop.
Extract images from a rosbag.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2016 Massachusetts Institute of Technology
"""Extract images from a rosbag.
"""
import os
import argparse
import cv2
import rosbag
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
def main():
"""Extract a folder of images from a rosbag.
"""
parser = argparse.ArgumentParser(description="Extract images from a ROS bag.")
parser.add_argument("bag_file", help="Input ROS bag.")
parser.add_argument("output_dir", help="Output directory.")
parser.add_argument("image_topic", help="Image topic.")
args = parser.parse_args()
print "Extract images from %s on topic %s into %s" % (args.bag_file,
args.image_topic, args.output_dir)
bag = rosbag.Bag(args.bag_file, "r")
bridge = CvBridge()
count = 0
for topic, msg, t in bag.read_messages(topics=[args.image_topic]):
cv_img = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
cv2.imwrite(os.path.join(args.output_dir, "frame%06i.png" % count), cv_img)
print "Wrote image %i" % count
count += 1
bag.close()
return
if __name__ == '__main__':
main()
@peteflorence
Copy link

great gist @wngreene, and also just found this from googling!

@kyelok
Copy link

kyelok commented Jul 25, 2018

@wngreene @peteflorence I just found this from googling as well lol

@abexultan
Copy link

Thank you!

@Babaevzhu
Copy link

good job, thanks!

@GenuineYZR
Copy link

Thanks, it's cool!

@FYamazaki
Copy link

Great. Thank you.

@paramaggarwal
Copy link

If you use ros-perception then just run rosrun image_view extract_images image:=<topic>. Documentation here.

@Akashbaskaran
Copy link

When I try this out, the images are getting saved as grayscale images. Is there any way I can get rgb images?

@lhoangan
Copy link

lhoangan commented May 16, 2019

To export RGB, change desired_encoding to "bgr8", more sophisticated example can be seen here

@DylanDC-C
Copy link

I had the same problem while using a Basler ace camera, even using rqt_bag didn't give colour images. If the solution above doesn't work for you take a look at this webpage for some help: http://wiki.ros.org/image_proc .
It runs a node that gives you another topic that produces colour images you can pull from.

@ghicheon
Copy link

great!! thanks!

@leviresende
Copy link

Congrats man! It works perfectly!

@NagabhushanSN95
Copy link

Please add information or links on how to install cv_bridge and rosbag.
Thanks!

@vim5818
Copy link

vim5818 commented Jun 16, 2020

Hello @wngreene, The code is flawless. It is great if have only to extract images from a single rosbag only once, I do not have the freedom to edit the name or the starting number of my extracted images. If i run the code on multiple rosbags, it overwrites the existing/previously created image. Please share some update on how to control the naming of the frame during extracting. !
Thank you

@Zach0806
Copy link

Zach0806 commented Sep 6, 2020

Hello @wngreene, I have run the code and it occurs this error: TypeError: init() takes 2 positional arguments but 3 were given. Do you know how to solve this problem?
Thank you!

@shanjiuvspikaqiu
Copy link

THX! very useful!

@makarandmandolkar
Copy link

makarandmandolkar commented Feb 3, 2021

This method doesn't work on compressed messages. Tweaking this code with only subscriber and playing the rosbag can work

@matteopantano
Copy link

Pretty cool:)

@domysticnom
Copy link

I'm trying to import rosbag, but it can't find the module, can someone share how you installed the module?

@kpthangalakshmi
Copy link

how can i use cv_bridge in anaconda(windows 10)? i am getting error like this
ERROR: Could not find a version that satisfies the requirement cvbridge (from versions: none)
ERROR: No matching distribution found for cvbridge

@Merwanski
Copy link

Thank you for the share

@monajalal
Copy link

@wngreene do you have a version of script for ros2 Humble? Thank you.

@AnishNavalgund
Copy link

@monajalal If you have a script for ros2 Humble, do share it! Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment