Skip to content

Instantly share code, notes, and snippets.

@lamhoangtung
Created December 30, 2018 14:52
Show Gist options
  • Save lamhoangtung/ba2620a3def328cdb6c576d613a42066 to your computer and use it in GitHub Desktop.
Save lamhoangtung/ba2620a3def328cdb6c576d613a42066 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import rospy
# ROS Image message
from sensor_msgs.msg import CompressedImage
# ROS Image message -> OpenCV2 image converter
from cv_bridge import CvBridge, CvBridgeError
# OpenCV2 for saving an image
import cv2
# Instantiate CvBridge
bridge = CvBridge()
def image_callback(unity_image):
print("Received an image!")
try:
# Convert your ROS Image message to OpenCV2
cv2_img = bridge.imgmsg_to_cv2(unity_image)
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
# cv2.imwrite('camera_image.jpeg', cv2_img)
cv2.imshow('Camera_feed', cv2_img)
def main():
rospy.init_node('python_image_subscriber', anonymous=True)
# Set up your subscriber and define its callback
rospy.Subscriber("/Team1_image/compressed", CompressedImage, image_callback)
# Spin until ctrl + c
rospy.spin()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment