- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert Matplotlib figure to PIL Image object?
To convert matplotlib figure to PIL image object, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a new figure or activate an existing figure.
- Plot a list using plot() method.
- Initialize the in-memory buffer.
- Save the buffered image.
- Use PIL image to get the image object.
- Show the current image.
- Close the in-memory I/O buffer.
Example
import io from PIL import Image import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.figure() plt.plot([1, 2]) img_buf = io.BytesIO() plt.savefig(img_buf, format='png') im = Image.open(img_buf) im.show(title="My Image") img_buf.close()
Output
- Related Articles
- How to convert a Torch Tensor to PIL image?
- How to open PIL Image in Tkinter on Canvas?
- Matplotlib figure to image as a numpy array
- How to set the current figure in Matplotlib?
- PyTorch – How to convert an image to grayscale?
- Embedding an Image in a Tkinter Canvas widget using PIL
- How to plot a watermark image in Matplotlib?
- How to convert an RGB image to HSV image using OpenCV Python?
- OpenCV Python – How to convert a colored image to a binary image?
- How to retrieve XY data from a Matplotlib figure?
- How to position and align a Matplotlib figure legend?
- How to retrieve colorbar instance from figure in Matplotlib?
- How to set the margins of a Matplotlib figure?
- How to add a 3d subplot to a matplotlib figure?\n
- How to convert Byte Array to Image in java?

Advertisements