2

I'm grabbing frames from a video file as following:

def capture_frame(file):
        capture = cv.CaptureFromFile("video.mp4")
        cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC)
        cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC, 90000)
        frame = cv.QueryFrame(capture)
        return frame

The frame type is cv2.cv.iplimage. How can I convert this type of image to jpeg image without saving?

Thanks,

7
  • 1
    I think you can only convert to JPEG when you save! JPEG is compression procedure not opencv image type Commented Nov 4, 2015 at 16:58
  • 1
    as I already mentioned in your previous question, you should use imencode Commented Nov 4, 2015 at 17:44
  • I couldn't find a good example which shows how I can use it. Commented Nov 4, 2015 at 17:45
  • 1
    stackoverflow.com/a/25592959/5008845 Commented Nov 4, 2015 at 17:58
  • I get segmentation fault error after using the method above Commented Nov 4, 2015 at 17:59

2 Answers 2

1

The following should give you the bytes for a jpeg representation of the image.

def capture_frame(file):
        capture = cv.CaptureFromFile(file)
        cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC)
        cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC, 90000)
        frame = cv.QueryFrame(capture)
        return cv.EncodeImage('.jpg', frame).tostring()

capture_frame("video.mp4")

I've written out the results of EncodeImage to a file opened in binary (open('somefile','wb')) which resulted in a valid JPEG.

Sign up to request clarification or add additional context in comments.

1 Comment

FYI cv2 now has .imencode() instead of .EncodeImage()
0

Did you try just writing chunks?

with open('filename.jpeg', 'wb+') as destination:
            for chunk in image_file.chunks():
                destination.write(chunk)
 

Here's one worth looking at too that uses opencv natively http://answers.opencv.org/question/115/opencv-python-save-jpg-specifying-quality-gives-systemerror/. Although, not sure why you're trying to save .mp4 to .jpeg...

2 Comments

Andrew, you misunderstood me. I don't want to save the file. I have to grab the frame, and send it as a jpg file to another function as a parameter.
I wish you actually answered the original question, because I'm stuck on this as well. I need to convert an cv2.cv.iplimage to a jpeg/array-of-bytes. So far I can't find anything on stack overflow that actually does this.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.