Image Sequence to Movie using python script


(nbunderson) #1

I want to use blender to convert a folder full of images into a movie. I can do this with the gui without any problems but when I follow (what I think are) the same steps in a python script it fails as I describe below. I posted this here a few days ago but I was flagged for spam and banned and so the post disappeared into the ether. Hopefully 2nd time is the charm.

I’ve followed all the help I was able to find online in regards to creating a movie from an image sequence using python scripting but was unable to get this to work. I have a sequence of images in a folder and I run blender from a command line like this:

blender -b empty.blend -P blepys.py FOLDERNAME

where FOLDERNAME is the path to the folder that has the images

the script blepys.py is here:

#-----blepys.py-----------------------------------------------------------
import bpy
import os
import sys

imdir = str(sys.argv[6]);

resx = 720; #1920
resy = 480; #1080
bpy.data.scenes[“Scene”].render.resolution_x = resx
bpy.data.scenes[“Scene”].render.resolution_y = resy
bpy.data.scenes[“Scene”].render.resolution_percentage = 100

file = [{“name”:i} for i in os.listdir(imdir)]
n = len(file)
print(n)
a = bpy.ops.sequencer.image_strip_add( \
directory = imdir, \
files = file, \
channel=1,
frame_start=0, \
frame_end=n-1)

stripname=file[0].get(“name”);
bpy.data.scenes[“Scene”].frame_end = n
bpy.data.scenes[“Scene”].render.image_settings.file_format = ‘AVI_JPEG’
bpy.data.scenes[“Scene”].render.filepath = ‘test.avi’
bpy.ops.render.render( animation=True )

Diagnostic to check whether the images were loaded

stripname=file[0].get(“name”);
print(bpy.data.scenes[“Scene”].sequence_editor.sequences[stripname])
print(dir(bpy.data.scenes[“Scene”].sequence_editor.sequences[stripname]))

#----------------------------------------------------------------

What happens::

  1. It does create a movie that lasts the expected length but the movie is a black screen for the entire duration. here is the truncated output while it is creating the movie:

Created avi: test.avi
Fra:1 Mem:6.00M (0.00M, Peak 8.64M) Sce: Ve:0 Fa:0 La:0
Fra:1 Mem:6.00M (0.00M, Peak 8.64M) Sce: Ve:0 Fa:0 La:0
Append frame 1 Time: 00:00.02 (Saving: 00:00.01)

… [this is repeated until]…

Fra:30 Mem:6.00M (0.00M, Peak 9.63M) Sce: Ve:0 Fa:0 La:0
Fra:30 Mem:6.00M (0.00M, Peak 9.63M) Sce: Ve:0 Fa:0 La:0
Append frame 30 Time: 00:00.01 (Saving: 00:00.01)

  1. The diagnostic lines i put at the end are my crude way of testing that the sequence loaded. They print the following so it seems like it did load:

<bpy_struct, ImageSequence(“BIOMECHANICO00000.png”)>
[‘doc’, ‘module’, ‘slots’, ‘alpha_mode’, ‘animation_offset_end’, ‘an
imation_offset_start’, ‘bl_rna’, ‘blend_alpha’, ‘blend_type’, ‘channel’, ‘color_
multiply’, ‘color_saturation’, ‘colorspace_settings’, ‘crop’, ‘directory’, ‘effe
ct_fader’, ‘elements’, ‘frame_duration’, ‘frame_final_duration’, ‘frame_final_en
d’, ‘frame_final_start’, ‘frame_offset_end’, ‘frame_offset_start’, ‘frame_start’
, ‘frame_still_end’, ‘frame_still_start’, ‘lock’, ‘modifiers’, ‘mute’, ‘name’, ’
proxy’, ‘rna_type’, ‘select’, ‘select_left_handle’, ‘select_right_handle’, ‘spee
d_factor’, ‘strip_elem_from_frame’, ‘strobe’, ‘swap’, ‘transform’, ‘type’, ‘upda
te’, ‘use_crop’, ‘use_default_fade’, ‘use_deinterlace’, ‘use_flip_x’, ‘use_flip_
y’, ‘use_float’, ‘use_linear_modifiers’, ‘use_proxy’, ‘use_proxy_custom_director
y’, ‘use_proxy_custom_file’, ‘use_reverse_frames’, ‘use_translation’]

I’m using 2.68 on Windows

Any help is very very appreciated


(Atom) #2

You may need to take more care in constructing your frame list. One invalid file will ruin your whole day. What I noticed on my Windows machine is that even though I could not see thumb.db in the File Explorer, it still existed as a hidden/invisible file in the folder.

Your code blindly included that non-image file in the list provided to image_strip_add, which failed.

By filtering your image list for specific file extensions you can avoid such a case and lead to success.


import bpy 
import os 
import sys 


#imdir = str(sys.argv[6]); 


in_dir = "C:\\Users\\Admin\\Documents\\Maps\\Animated\\explosion_field"
lst = os.listdir(in_dir)


out_dir = "C:\\Users\\Admin\\Desktop\	est.avi"


resx = 720; #1920 
resy = 480; #1080 
bpy.data.scenes["Scene"].render.resolution_x = resx 
bpy.data.scenes["Scene"].render.resolution_y = resy 
bpy.data.scenes["Scene"].render.resolution_percentage = 100 


# Filter file list by valid file types.
candidates = []
c = 0
for item in lst:
    fileName, fileExtension = os.path.splitext(lst[c])
    if fileExtension == ".png":
        candidates.append(item)
    c = c + 1


file = [{"name":i} for i in candidates]   
n = len(file) 
print(n)   
a = bpy.ops.sequencer.image_strip_add(directory = in_dir, files = file, channel=1, frame_start=0, frame_end=n-1) 


stripname=file[0].get("name"); 
bpy.data.scenes["Scene"].frame_end = n 
bpy.data.scenes["Scene"].render.image_settings.file_format = 'AVI_JPEG' 
bpy.data.scenes["Scene"].render.filepath = out_dir 
bpy.ops.render.render( animation=True ) 


# Diagnostic to check whether the images were loaded 
stripname=file[0].get("name"); 
print(bpy.data.scenes["Scene"].sequence_editor.sequences[stripname]) 
print(dir(bpy.data.scenes["Scene"].sequence_editor.sequences[stripname])) 

When I run this code on my machine I get a valid AVI file that plays the image sequence as a movie.

NOTE: This code may fail if your images have an upper case file extension, so adjust the candidate IF statement as needed.


(nbunderson) #3

It turns out I didn’t need to filter the image file type, but you gave me the answer. I needed the full path in what I called “imdir” and you called “in_dir”. Thank you!