Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: support matplotlib 3.3.0 #2754

Merged
merged 3 commits into from
Jul 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions yt/utilities/png_writer.py
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good solution for the imports. Thanks for adding the message too for our future devs. 🙂

# 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the width and height were passed to the previous function when it seems like it didn't use them? Thanks for cleaning that up.



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