Images to video - converting to IplImage makes video blue

Posted by user891908 on Stack Overflow See other posts from Stack Overflow or by user891908
Published on 2012-09-14T06:25:58Z Indexed on 2012/09/15 15:38 UTC
Read the original article Hit count: 231

Filed under:
|

I want to create a video from images using opencv.

The strange problem is that if i will write image (bmp) to disk and then load (cv.LoadImage) it it renders fine, but when i try to load image from StringIO and convert it to IplImage, it turns video to blue.

Heres the code:

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

© Stack Overflow or respective owner

Related posts about python

Related posts about opencv