Open In App

How to Save Images from Python PyVista

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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'.

Python
import pyvista as pv

# creating a sphere mesh
sphere = pv.Sphere()

# setting up the plotter
plotter = pv.Plotter(off_screen=True)  # using off_screen=True for off-screen rendering

# adding the sphere mesh to the plotter
plotter.add_mesh(sphere, color='blue')

# setting the camera position
plotter.camera_position = 'xy'

# saving the plot as an image
plotter.screenshot('sphere.png')

print("Sphere image saved as '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'.

Python
import pyvista as pv

# creating sample meshes
sphere = pv.Sphere(center=(0, 0, 0), radius=0.5)
cube = pv.Cube(center=(1, 1, 1), x_length=0.5, y_length=0.5, z_length=0.5)
cone = pv.Cone(center=(-1, -1, -1), direction=(0, 1, 0), height=1, radius=0.5)

# setting up the plotter
plotter = pv.Plotter(off_screen=True)  # using off_screen=True for off-screen rendering

# adding the meshes to the plotter
plotter.add_mesh(sphere, color='blue', label='Sphere')
plotter.add_mesh(cube, color='green', label='Cube')
plotter.add_mesh(cone, color='red', label='Cone')

# adding a legend
plotter.add_legend()

# setting the camera position
plotter.camera_position = 'iso'

# saving the plot as an image
plotter.screenshot('multiple_meshes.png')

print("Image with multiple meshes saved as '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'.

Python
import pyvista as pv
# create a sphere mesh
sphere = pv.Sphere()
# set up the plotter
plotter = pv.Plotter(off_screen=True)  # use off_screen=True for off-screen rendering
# add the sphere mesh to the plotter
plotter.add_mesh(sphere, color='red')
# set the camera position
plotter.camera_position = 'xy'
# customize resolution and background color
plotter.window_size = [1920, 1080]  # set resolution to 1920x1080
plotter.set_background('white')     # set background color to white
# save the plot as an image
plotter.screenshot('custom_sphere.png')
print("Customized sphere image saved 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'.

Python
import pyvista as pv
# creating a cube mesh
cube = pv.Cube()
# setting up the plotter
plotter = pv.Plotter(off_screen=True)  # Use off_screen=True for off-screen rendering
# adding the cube mesh to the plotter
plotter.add_mesh(cube, color='green')
# setting the camera position
plotter.camera_position = 'iso'  # Set to isometric view
# lighting customization
plotter.add_light(pv.Light(position=(1, 1, 1), focal_point=(0, 0, 0), color='white'))
# adding annotations
plotter.add_text('Cube Visualization', position='upper_edge', color='black', font_size=12)
# saving the plot as an image
plotter.screenshot('custom_cube.png')
print("Customized cube image with lighting and annotations saved 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.


Next Article
Article Tags :
Practice Tags :

Similar Reads