Moviepy: CompositeVideoClip fails with gray-scale ImageClip images

Created on 29 Jul 2017  路  5Comments  路  Source: Zulko/moviepy

When composing a gray-scale image with a color video clip the writing of the result fails with a ValueError.

Example code

```.py
video = VideoFileClip('myHolidays.mp4')

Image from https://en.wikipedia.org/wiki/Unix#/media/File:Ken_Thompson_(sitting)_and_Dennis_Ritchie_at_PDP-11_(2876612463).jpg

img = ImageClip('Ken_Thompson_(sitting)_and_Dennis_Ritchie_at_PDP-11_(2876612463).jpg', duration=video.duration)
final = CompositeVideoClip([img, video])
final.write_videofile('result.mp4')


## Stack trace

File "pip.py", line 59, in
final.write_videofile('foo.mp4')
File "", line 2, in write_videofile
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\decorators.py", line 54, in requires_duration
return f(clip, a, *k)
File "", line 2, in write_videofile
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\decorators.py", line 137, in use_clip_fps_by_default
return f(clip, new_a, *new_kw)
File "", line 2, in write_videofile
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\decorators.py", line 22, in convert_masks_to_RGB
return f(clip, a, *k)
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\video\VideoClip.py", line 324, in write_videofile
progress_bar=progress_bar)
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\video\io\ffmpeg_writer.py", line 209, in ffmpeg_write_video
fps=fps, dtype="uint8"):
File "C:\Anaconda3\lib\site-packages\tqdm_tqdm.py", line 833, in __iter__
for obj in iterable:
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\Clip.py", line 479, in generator
frame = self.get_frame(t)
File "", line 2, in get_frame
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\decorators.py", line 89, in wrapper
return f(new_a, *new_kw)
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\Clip.py", line 95, in get_frame
return self.make_frame(t)
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\video\compositing\CompositeVideoClip.py", line 110, in make_frame
f = c.blit_on(f, t)
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\video\VideoClip.py", line 574, in blit_on
return blit(img, picture, pos, mask=mask, ismask=self.ismask)
File "C:\Anaconda3\lib\site-packages\moviepy-0.2.3.2-py3.5.egg\moviepy\video\tools\drawing.py", line 47, in blit
new_im2[yp1:yp2, xp1:xp2] = blitted
ValueError: could not broadcast input array from shape (1080,1649) into shape (1080,1649,3)
```

3rd-party bug images video

Most helpful comment

A quick workaround using PIL to convert grayscale to RGB:

            from PIL import Image
            formatter = {"PNG": "RGBA", "JPEG": "RGB"}
            img = Image.open(file_name)
            rgbimg = Image.new(formatter.get(img.format, 'RGB'), img.size)
            rgbimg.paste(img)
            rgbimg.save(file_name, format=img.format)

All 5 comments

+1

A quick workaround using PIL to convert grayscale to RGB:

            from PIL import Image
            formatter = {"PNG": "RGBA", "JPEG": "RGB"}
            img = Image.open(file_name)
            rgbimg = Image.new(formatter.get(img.format, 'RGB'), img.size)
            rgbimg.paste(img)
            rgbimg.save(file_name, format=img.format)

I think we should have such option as keyword at ImageSequenceClip

+1

This is the workaround I added, call convert_to_RGB(filePath) on Gray scale image:

import cv2

def convert_to_RGB(media):
    imgtype = os.popen('identify ' + media +
                       ' | grep Gray').read().rstrip()
    if imgtype != '':
        img = cv2.imread(media)
        (b, g, r) = cv2.split(img)
        img = cv2.merge([r, g, b])te
        cv2.imwrite(media, img)

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LaoYuanPython picture LaoYuanPython  路  3Comments

Netherdrake picture Netherdrake  路  5Comments

Swiffers picture Swiffers  路  4Comments

cquintini picture cquintini  路  4Comments

djevo1 picture djevo1  路  3Comments