OpenCV Python bindings: How do I capture an image from memory
Asked Answered
M

1

2

I have multiple jpeg images stored as strings in memory. I want to generate a video from them (so those pictures would be the frames in the video).

How can I do that?

Mertz answered 21/1, 2013 at 12:58 Comment(1)
what do you mean by "jpeg images stored as strings in memory"? you mean the name of some jpeg files are given? or the content of these images are in the memory?Overplus
L
4

If you have a collection of strings that individually represents one image each, you can combine StringIO with PIL to read it without saving them to files. Then you convert from a PIL image to OpenCV by using numpy. Also, update your code to use the new OpenCV bindings.

Here is an example that duplicates what you have (sys.argv[1] assumed to be the name of the output video file), you might need to adjust the video codec:

import sys
import numpy
import cv
import cv2
from cStringIO import StringIO
from PIL import Image

out_video = cv2.VideoWriter()
fourcc = cv.CV_FOURCC('D', 'I', 'V', 'X')
fps = 30
color = True

size = None
for fname in sys.argv[2:]:
    data = open(fname).read() # Your String

    s = StringIO(data)
    img = Image.open(s)
    if size and img.size != size:
        img = img.resize(size)
    else:
        size = img.size
        out_video.open(sys.argv[1], fourcc, fps, size, color)
    out_video.write(cv2.cvtColor(numpy.array(img), cv2.COLOR_RGB2BGR))
Longsome answered 21/1, 2013 at 13:13 Comment(12)
Oh my God... I already have the image data in memory, and I want to make a video out of it. So now I'll have to write it to a temporary file, read it into memory again, and then write the video to a file. That really sucks.Mertz
Sorry, I am baffled by your comment. Do you mean it's good to save the video to a file? I do want to save the video to a file, just not happy about having to save the images to a file.Mertz
@RamRachum I'm baffled by your comment too, what images are you talking about now ? Wasn't the question about opening a video ? Or maybe you want me to rewrite opencv to match your needs ? The C/C++ bindings do not accept file streams either.Longsome
Look at my question and answer the following 2 questions: 1. How many times does the word "video" appear? 2. How many times does the word "image" appear?Mertz
@RamRachum are you aware of what CaptureFromFile is used for ?Longsome
I've used it to load an image and then later write it (and a bunch of other images) to a video. I saw it on Stack Overflow.Mertz
@RamRachum it is used to load a video file which is then decomposed into a sequence of frames (which are images). Can you be a little more specific on what you want to do ? Apparently you don't have a video as input, and to construct a video as output you don't need to save the images as files. Clarify what you want please.Longsome
I want to take a bunch of images and make a video out of them. (I saw an answer on SO recommending to use CaptureFromFile). If you know of a better way I'll be happy to hear it.Mertz
@RamRachum provide a link to the actual question/answer so I can make sense of what you are doing.Longsome
@RamRachum that code makes no sense to me, the person is writing the same image n times to build a video that is nothing but the single image being shown for some given time. So, what do you really want to do ? Update your question with a better explanation, because right now it is meaningless given this discussion.Longsome
I already said what I want to do. It's very simple. I have multiple jpeg images stored as strings in memory. I want to generate a video from them (so those pictures would be the frames in the video). That's it. What more information is needed?Mertz
@RamRachum this exact information that you just mentioned needs to be in your question, because it is not. Rewrite your question to say exactly what you just said and it becomes clear.Longsome

© 2022 - 2024 — McMap. All rights reserved.