From: =?utf-8?q?Cl=C3=A9ment_Robert?= <cr52@protonmail.com>
Date: Fri, 17 Jul 2020 11:28:14 +0200
Subject: Support compatibility with matplotlib's latest version

(3.3.0) by using pillow instead of the old _png module

https://github.com/yt-project/yt/pull/2754

Closes: #967000
---
 yt/utilities/png_writer.py | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/yt/utilities/png_writer.py b/yt/utilities/png_writer.py
index bafa2f2..7fb92fc 100644
--- a/yt/utilities/png_writer.py
+++ b/yt/utilities/png_writer.py
@@ -10,7 +10,14 @@ Writing PNGs
 # The full license is in the file COPYING.txt, distributed with this software.
 #-----------------------------------------------------------------------------
 
-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
+
 from yt.extern.six import PY2
 
 if PY2:
@@ -18,20 +25,21 @@ if PY2:
 else:
     from io import BytesIO as StringIO
 
-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), format="png")
+    
 
 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 = StringIO()
-    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
