简体   繁体   English

OpenCV Python绑定:如何从内存中捕获图像

[英]OpenCV Python bindings: How do I capture an image from memory

I have multiple jpeg images stored as strings in memory. 我有多个jpeg图像以字符串形式存储在内存中。 I want to generate a video from them (so those pictures would be the frames in the video). 我想从它们生成视频(因此这些图片就是视频中的帧)。

How can I do that? 我怎样才能做到这一点?

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. 如果您有一个字符串集合,每个字符串分别代表一个图像,则可以将StringIOPIL组合以读取它,而无需将它们保存到文件中。 Then you convert from a PIL image to OpenCV by using numpy . 然后,您可以使用numpyPIL图像转换为OpenCV。 Also, update your code to use the new OpenCV bindings. 另外,更新代码以使用新的OpenCV绑定。

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: 下面是一个示例,该示例复制了现有内容(假定sys.argv [1]为输出视频文件的名称),您可能需要调整视频编解码器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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