yusuf
yusuf

Reputation: 3781

conversion from opencv image to jpeg image in python

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,

Upvotes: 2

Views: 6491

Answers (2)

AndrewSmiley
AndrewSmiley

Reputation: 1963

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...

Upvotes: 0

djsumdog
djsumdog

Reputation: 2710

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.

Upvotes: 1

Related Questions