-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fig2image convenience utility function
- Loading branch information
1 parent
4572ee2
commit b7b4011
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import io | ||
|
||
import PIL.Image | ||
|
||
|
||
def fig2img(fig): | ||
"""Matplotlib Figure to PIL Image | ||
Convert a Matplotlib figure to a PIL Image | ||
Parameters | ||
---------- | ||
fig : plt.figure | ||
Matplotlib figure. | ||
Returns | ||
---------- | ||
list | ||
The rescaled values. | ||
Examples | ||
---------- | ||
>>> import pyllusion | ||
>>> import matplotlib.pyplot as plt | ||
>>> | ||
>>> plt.plot([1, 2, 3, 4, 5]) | ||
>>> fig = plt.gcf() | ||
>>> fig2img(fig) | ||
""" | ||
buffer = io.BytesIO() | ||
fig.savefig(buffer) | ||
buffer.seek(0) | ||
img = PIL.Image.open(buffer) | ||
return img |