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?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • 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? – fireant Jan 25 '13 at 07:44

1 Answers1

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))
mmgp
  • 18,901
  • 3
  • 53
  • 80
  • 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. – Ram Rachum Jan 21 '13 at 13:42
  • 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. – Ram Rachum Jan 21 '13 at 14:10
  • @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. – mmgp Jan 21 '13 at 14:53
  • 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? – Ram Rachum Jan 21 '13 at 17:15
  • @RamRachum are you aware of what `CaptureFromFile` is used for ? – mmgp Jan 21 '13 at 17:18
  • 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. – Ram Rachum Jan 21 '13 at 17:42
  • @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. – mmgp Jan 21 '13 at 17:50
  • 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. – Ram Rachum Jan 21 '13 at 17:53
  • @RamRachum provide a link to the actual question/answer so I can make sense of what you are doing. – mmgp Jan 21 '13 at 20:16
  • @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. – mmgp Jan 21 '13 at 21:37
  • 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? – Ram Rachum Jan 21 '13 at 21:53
  • @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. – mmgp Jan 21 '13 at 21:55