VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-save-images-from-python-pyvista/

⇱ How to Save Images from Python PyVista - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Save Images from Python PyVista

Last Updated : 23 Jul, 2025

PyVista is a powerful and versatile library for 3D visualization in Python. It is built on top of the Visualization Toolkit (VTK) and provides an intuitive and user-friendly API for creating, manipulating, and visualizing 3D data. One of the common tasks when working with 3D visualizations is saving images of the rendered scenes. This article will guide you through the process of saving images from PyVista.

Setting Up PyVista for Image Saving

Firstly, we need to make sure that PyVista is installed on our system. We can install it using PIP Package Manager. Open the VSCode terminal and execute the below installation command.

pip install pyvista

We also need to install some other additional dependencies like numpy and vtk. So execute the below command to install all the required dependencies.

pip install numpy vtk

Saving Plots as Images

In this section, we will explore various examples to save plots as images using PyVista in Python.

Example 1: Saving a Simple Sphere Plot

In this example, we are using PyVista to create and save an image of a 3D sphere. We start by creating a sphere mesh and setting up a plotter for off-screen rendering. The sphere mesh is added to the plotter with a blue color, and the camera is positioned to view the sphere from the 'xy' plane. Finally, the plot is saved as an image file named 'sphere.png'.

Output:

👁 OP1
Sphere Image Saved
👁 OP2
Sphere Image

Example 2: Saving a Plot with Multiple Meshes

In this example, we are using PyVista to create and save an image of a plot containing multiple 3D meshes: a sphere, a cube, and a cone. Each mesh is added to the plotter with distinct colors and labels, and a legend is included for reference. The camera is positioned to an isometric view, and the plot is saved as an image file named 'multiple_meshes.png'.

Output:

👁 OP1
Image with Multiple Meshes
👁 OP2
Multiple Meshes

Customizing Image Output

In this section, we will customize the Image Output using various properties provided by PyVista library.

Example 1: Customizing Resolution and Background Color

We are customizing the resolution and background color of a PyVista plot of a red sphere. The plot's resolution is set to 1920x1080, and the background color is changed to white before saving the image as 'custom_sphere.png'.

Output:

👁 OP1

Example 2: Customizing Lighting and Adding Annotations

In this example, we are customizing the lighting and adding annotations to a PyVista plot of a green cube. A light source is added for enhanced lighting, and text is annotated on the plot. The camera is set to an isometric view before saving the image as 'custom_cube.png'.

Output:

👁 OP2

Conclusion

In conclusion, saving images from PyVista involves creating and customizing 3D visualizations, setting up a plotter for off-screen rendering, and utilizing various PyVista features such as mesh addition, camera positioning, and image customization. This allows for efficient capturing and sharing of complex 3D models.

Comment
Article Tags: