How to Save Images from Python PyVista
Last Updated :
24 Jul, 2024
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:
Sphere Image Saved
Sphere ImageExample 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:
Image with Multiple Meshes
Multiple MeshesCustomizing 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:
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:
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.
Similar Reads
How to read image from SQL using Python?
In this article, we are going to discuss how to read an image or file from SQL using python. For doing the practical implementation, We will use MySQL database. Â First, We need to connect our Python Program with MySQL database. For doing this task, we need to follow these below steps: Steps to Conne
3 min read
How to open an image from the URL in PIL?
In this article, we will learn How to open an image from the URL using the PIL module in python. For the opening of the image from a URL in Python, we need two Packages urllib and Pillow(PIL). Approach:Install the required libraries and then import them. To install use the following commands:pip ins
1 min read
How to extract images from PDF in Python?
The task in this article is to extract images from PDFs and convert them to Image to PDF and PDF to Image in Python.To extract the images from PDF files and save them, we use the PyMuPDF library. First, we would have to install the PyMuPDF library using Pillow.pip install PyMuPDF PillowPyMuPDF is us
3 min read
How to Use PyVista Plotter for 3D Visualization in Python
In Python, PyVista is a powerful library for 3D visualization and mesh analysis. It integrates seamlessly with NumPy and provides a robust set of tools for creating interactive plots, mesh generation, and advanced visualization techniques. PyVista's Plotter class offers a versatile environment to cr
3 min read
Extract images from video in Python
OpenCV comes with many powerful video editing functions. In current scenario, techniques such as image scanning, face recognition can be accomplished using OpenCV. Image Analysis is a very common field in the area of Computer Vision. It is the extraction of meaningful information from videos or imag
2 min read
How to create Word Art from an image using Python?
In this article, we are going to learn how to create word art from an image using Python. In this, we are going to take an image as input and then created a similar image in a text form using the characters. We can perform this task with the help of pillow and pywhatkit modules of Python. Pillow Thi
2 min read
How to Read Image in SQLite using Python?
This article shows us how to use the Python sqlite3 module to read or retrieve images that are stored in the form of BLOB data type in an SQLite table. First, We need to read an image that is stored in an SQLite table in BLOB format using python script and then write the file back to any location on
3 min read
How to capture a image from webcam in Python?
In this article, we will discuss how to capture an image from the webcam using Python. We will use OpenCV and PyGame libraries. Both libraries include various methods and functions to capture an image and video also. By using, these vast libraries we need to write only 4 to 5 lines of code to captur
3 min read
Python - How to save canvas in pyqt5?
There are so many options provided by Python to develop GUI application and PyQt5 is one of them. PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library
3 min read
How to resize Image in Python - Tkinter?
Python provides multiple options for building GUI (Graphical User Interface) applications. Among them, Tkinter is one of the most widely used and simplest options. It comes bundled with Python and serves as a standard interface to the Tk GUI toolkit.However, Tkinter alone does not provide support fo
2 min read