Fast way to create an animation from binary data

I have a C++ program which writes pixel data for a 2D grid to a binary file. Each binary file may have numerous grid states back-to-back, and there may be multiple of these binary files.

e.g. I could have 10 binary files bin0, bin1, bin2… bin9 each holding data for 10 grid states for a total of 100 grid states to animate.

I’m looking for a fast way to create an animation from the grid states in these binary files.

My first attempt used python and PIL.Image to create a gif:

import glob
import numpy as np
from PIL import Image

paths = glob.glob('./outfiles/dump*')
imgs = []
for path in sorted(paths, key=lambda x: int(x.split("p")[1])): 
    data = np.fromfile(path, dtype=np.int8)
    n = int(data.size / (rows*cols))
    for i in range(n):
        offset = i*rows*cols
        state = np.reshape(data[offset:offset+rows*cols], (rows, cols))
        img = Image.fromarray(state*255)
        imgs.append(img)

imgs[0].save('./animation.gif', save_all=True, append_images=imgs[1:], loop=0)

but it runs pretty slow (I could have thousands of grid states to animate). line_profiler shows that Image.fromarray() is the bottleneck (about 80% of runtime). I also recognise I could easily run into memory problems having to store every grid state in a list.

Looking forward to suggestions for how to make this run fast. Not bothered about sticking to Python if there is a better alternative using C/C++ (or other languages, but Python or C/C++ preferred), nor does the animation have to be a gif.

Thanks

Source: Windows Questions C++

LEAVE A COMMENT