Skip to content

Commit

Permalink
add fig2image convenience utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Dec 16, 2023
1 parent 4572ee2 commit b7b4011
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyllusion/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Pyllusion submodule.
"""

from .fig2image import fig2image
from .image_blob import image_blob, image_blobs
from .image_circle import image_circle, image_circles
from .image_line import image_line
Expand All @@ -24,4 +25,5 @@
"image_mosaic",
"image_scramble",
"rescale",
"fig2image"
]
36 changes: 36 additions & 0 deletions pyllusion/image/fig2image.py
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

0 comments on commit b7b4011

Please sign in to comment.