-
-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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,46 @@ | ||
import io | ||
|
||
|
||
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 | ||
---------- | ||
.. ipython:: python | ||
import neurokit2 as nk | ||
import matplotlib.pyplot as plt | ||
plt.plot([1, 2, 3, 4, 5]) # Make plot | ||
fig = plt.gcf() # Get current figure | ||
nk.fig2img(fig) # Convert to PIL Image | ||
plt.close(fig) # Close figure | ||
""" | ||
|
||
try: | ||
import PIL.Image | ||
except ImportError as e: | ||
raise ImportError( | ||
"fig2img(): the 'PIL' (Pillow) module is required for this function to run. ", | ||
"Please install it first (`pip install pillow`).", | ||
) from e | ||
|
||
buffer = io.BytesIO() | ||
fig.savefig(buffer) | ||
buffer.seek(0) | ||
img = PIL.Image.open(buffer) | ||
return img |