How to convert Image to PIL.Image
How do you convert a ROS Image message data to a PIL Image?
I found code that works for CompressedImage, but it fails for the normal Image data type:
def convert(msg):
import io
from PIL import Image
return Image.open(io.BytesIO(bytearray(msg.data)))
which gives me the error:
IOError: cannot identify image file
I tried the cv_bridge component, since it's trivial to convert a CV image to PIL, but when I tried:
def convert(msg):
import cv2
bridge = CvBridge()
cv_image = bridge.imgmsg_to_cv2(msg, "rgb8")
cv2.imwrite('sample.jpg', cv_image)
I get a mangled image like:
What am I doing wrong?
Edit: It seems I can't even view the raw stream using image_view. If I run:
rosrun image_view image_view image:=/camera/image
it gives me the error:
[ERROR] [1458436979.679038299]: Unable to convert 'bgra8' image for display: 'Image is wrongly formed: height * step != size or 240 * 1360 != 337920'
What's wrong with my video stream?