我想使用 opencv 从图像创建视频。
奇怪的问题是,如果我将图像(bmp)写入磁盘然后加载(cv.LoadImage)它渲染得很好,但是当我尝试从 StringIO 加载图像并将其转换为 IplImage 时,它会将视频变为蓝色。
继承人的代码:
import StringIO
output = StringIO.StringIO()
FOREGROUND = (0, 0, 0)
TEXT = 'MY TEXT'
font_path = 'arial.ttf'
font = ImageFont.truetype(font_path, 18, encoding='unic')
text = TEXT.decode('utf-8')
(width, height) = font.getsize(text)
# Create with background with place for text
w,h=(600,600)
contentimage=Image.open('0.jpg')
background=Image.open('background.bmp')
x, y = contentimage.size
# put content onto background
background.paste(contentimage,(((w-x)/2),0))
draw = ImageDraw.Draw(background)
draw.text((0,0), text, font=font, fill=FOREGROUND)
pi = background
pi.save(output, "bmp")
#pi.show() #shows image in full color
output.seek(0)
pi= Image.open(output)
print pi, pi.format, "%dx%d" % pi.size, pi.mode
cv_im = cv.CreateImageHeader(pi.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, pi.tostring())
print pi.size, cv.GetSize(cv_im)
w = cv.CreateVideoWriter("2.avi", cv.CV_FOURCC('M','J','P','G'), 1,(cv.GetSize(cv_im)[0],cv.GetSize(cv_im)[1]), is_color=1)
for i in range(1,5):
cv.WriteFrame(w, cv_im)
del w