Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.49k stars 1.56k forks source link

CompositeVideoClip fails with gray-scale ImageClip images #623

Open dspinellis opened 7 years ago

dspinellis commented 7 years ago

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

Example code

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 <module>
    final.write_videofile('foo.mp4')
  File "<decorator-gen-51>", 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 "<decorator-gen-50>", 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 "<decorator-gen-49>", 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 "<decorator-gen-10>", 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)
tismagic commented 7 years ago

+1

pkarp0 commented 6 years ago

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)
kiddten commented 5 years ago

I think we should have such option as keyword at ImageSequenceClip

scirahul commented 4 years ago

+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)
astarrr commented 4 years ago

+1

gaurav-95 commented 1 year ago

For me pip install imageio==2.4.1 fixed it.

Although my images are not greyscale I used to get a similar error before this like so:

File "/home/ubuntu/neus/neusenv/lib/python3.8/site-packages/moviepy/video/tools/drawing.py", line 41, in blit new_im2[yp1:yp2, xp1:xp2] = blitted ValueError: could not broadcast input array from shape (400,450,2) into shape (400,450,3)

glovebx commented 10 months ago

For me pip install imageio==2.4.1 fixed it.

Although my images are not greyscale I used to get a similar error before this like so:

File "/home/ubuntu/neus/neusenv/lib/python3.8/site-packages/moviepy/video/tools/drawing.py", line 41, in blit new_im2[yp1:yp2, xp1:xp2] = blitted ValueError: could not broadcast input array from shape (400,450,2) into shape (400,450,3)

not working