简体   繁体   中英

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,

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM