-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2754 from neutrinoceros/hotfix_mpl3.3_support
hotfix: support matplotlib 3.3.0
- Loading branch information
Showing
1 changed file
with
15 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
from io import BytesIO | ||
|
||
import matplotlib._png as _png | ||
try: | ||
# matplotlib switched from an internal submodule _png to using pillow (PIL) | ||
# between v3.1.0 and v3.3.0 | ||
# So PIL should be available on any system where matplotlib._png doesn't exist | ||
import matplotlib._png as _png | ||
except ImportError: | ||
from PIL import Image | ||
|
||
|
||
def call_png_write_png(buffer, width, height, fileobj, dpi): | ||
_png.write_png(buffer, fileobj, dpi) | ||
def call_png_write_png(buffer, fileobj, dpi): | ||
try: | ||
_png.write_png(buffer, fileobj, dpi) | ||
except NameError: | ||
Image.fromarray(buffer).save(fileobj, dpi=(dpi, dpi)) | ||
|
||
|
||
def write_png(buffer, filename, dpi=100): | ||
width = buffer.shape[1] | ||
height = buffer.shape[0] | ||
with open(filename, "wb") as fileobj: | ||
call_png_write_png(buffer, width, height, fileobj, dpi) | ||
call_png_write_png(buffer, fileobj, dpi) | ||
|
||
|
||
def write_png_to_string(buffer, dpi=100, gray=0): | ||
width = buffer.shape[1] | ||
height = buffer.shape[0] | ||
def write_png_to_string(buffer, dpi=100): | ||
fileobj = BytesIO() | ||
call_png_write_png(buffer, width, height, fileobj, dpi) | ||
call_png_write_png(buffer, fileobj, dpi) | ||
png_str = fileobj.getvalue() | ||
fileobj.close() | ||
return png_str |