Skip to content

Video editing with Python

fix to work with python3 : following code example in http://matplotlib.org/examples/pylab_examples/to_numeric.html

Laurent Perrinet23a3dfe69263
diff --git a/moviepy/video/io/bindings.py b/moviepy/video/io/bindings.py
index 74f1da5c..f5c2a7e8 100644
--- a/moviepy/video/io/bindings.py
+++ b/moviepy/video/io/bindings.py
@@ -16,9 +16,16 @@ def PIL_to_npimage(im):
def mplfig_to_npimage(fig):
""" Converts a matplotlib figure to a RGB frame after updating the canvas"""
- fig.canvas.draw() # update/draw the elements
- w,h = fig.canvas.get_width_height()
- buf = fig.canvas.tostring_rgb()
+ from matplotlib.backends.backend_agg import FigureCanvasAgg
+
+ agg = fig.canvas.switch_backends(FigureCanvasAgg)
+ agg.draw() # update/draw the elements
+
+ # get the width and the height to resize the matrix
+ l,b,w,h = agg.figure.bbox.bounds
+ w, h = int(w), int(h)
+
+ buf = agg.tostring_rgb()
image= +np.fromstring(buf,dtype=np.uint8)
return image.reshape(h,w,3)