Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the v2 equivalent of moviepy.video.io.bindings.mplfig_to_npimage() ? #2297

Closed
tieneupin opened this issue Dec 24, 2024 · 2 comments
Closed
Labels
question Questions regarding functionality, usage

Comments

@tieneupin
Copy link

Our team has previously used moviepy to convert matplotlib figures into RGB images using the mplfig_to_npimage function back in version 1:

def make_video_frame():
    fig, ax = plt.subplots(...)
    ...  # Arbitrary plot of data
    return mplfig_to_npimage(fig)

Could you advise on what the version 2 equivalent of this function is? Thanks!

@tieneupin tieneupin added the question Questions regarding functionality, usage label Dec 24, 2024
@OsaAjani
Copy link
Collaborator

Hi, I dont think there is any equivalent, with the v2 we choose to reduce the scope and refocus on the main job of moviepy, video editing, any link to matplotlib have been dropped in the process, if you need it you'll have to dig it from previous version and implement it yourself, sorry.

@RRiva
Copy link

RRiva commented Jan 23, 2025

For anyone that will need this, I have copied the code from MoviePy 1 and updated it to Matplotlib 3.8.

# Copied from MoviePy 1, because it has been removed in version 2.
# https://github.com/Zulko/moviepy/blob/db19920764b5cb1d8aa6863019544fd8ae0d3cce/moviepy/video/io/bindings.py#L18C1-L32C32
# With some updates to Matplotlib 3.8.
def mplfig_to_npimage(fig):
    """ Converts a matplotlib figure to a RGB frame after updating the canvas"""
    from matplotlib.backends.backend_agg import FigureCanvasAgg
    canvas = FigureCanvasAgg(fig)
    canvas.draw() # update/draw the elements

    # get the width and the height to resize the matrix
    l,b,w,h = canvas.figure.bbox.bounds
    w, h = int(w), int(h)

    #  exports the canvas to a memory view and then to a numpy nd.array
    mem_view = canvas.buffer_rgba()  # Update to Matplotlib 3.8
    image = np.asarray(mem_view)
    return image[:, :, :3]  # Return only RGB, not alpha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions regarding functionality, usage
Projects
None yet
Development

No branches or pull requests

3 participants