From 79df70ac0b9f7d2c6778c6ed8764601a866dd1cd Mon Sep 17 00:00:00 2001 From: Mike Gimelfarb <35513382+mike-gimelfarb@users.noreply.github.com> Date: Sat, 21 Dec 2024 18:21:03 -0500 Subject: [PATCH 1/2] Switch to different buffering package --- pyRDDLGym/core/visualizer/chart.py | 9 +++++---- pyRDDLGym/core/visualizer/heatmap.py | 9 +++++---- pyRDDLGym/core/visualizer/text.py | 9 +++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pyRDDLGym/core/visualizer/chart.py b/pyRDDLGym/core/visualizer/chart.py index aeee7d38..16b5d587 100644 --- a/pyRDDLGym/core/visualizer/chart.py +++ b/pyRDDLGym/core/visualizer/chart.py @@ -1,3 +1,4 @@ +from io import BytesIO from PIL import Image import numpy as np import matplotlib.pyplot as plt @@ -136,10 +137,10 @@ def _setup(self): def convert2img(self, fig, ax): fig.canvas.draw() - data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) - data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) - img = Image.fromarray(data) - self._data = data + buf = BytesIO() + fig.savefig(buf, format='png') + buf.seek(0) + img = Image.open(buf) self._img = img return img diff --git a/pyRDDLGym/core/visualizer/heatmap.py b/pyRDDLGym/core/visualizer/heatmap.py index 39d12333..80b4af21 100644 --- a/pyRDDLGym/core/visualizer/heatmap.py +++ b/pyRDDLGym/core/visualizer/heatmap.py @@ -1,3 +1,4 @@ +from io import BytesIO from PIL import Image import matplotlib matplotlib.use('agg') @@ -98,10 +99,10 @@ def _setup(self): def convert2img(self, fig, ax): fig.canvas.draw() - data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) - data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) - img = Image.fromarray(data) - self._data = data + buf = BytesIO() + fig.savefig(buf, format='png') + buf.seek(0) + img = Image.open(buf) self._img = img return img diff --git a/pyRDDLGym/core/visualizer/text.py b/pyRDDLGym/core/visualizer/text.py index 379bbf36..774cf80f 100644 --- a/pyRDDLGym/core/visualizer/text.py +++ b/pyRDDLGym/core/visualizer/text.py @@ -1,3 +1,4 @@ +from io import BytesIO import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt @@ -38,10 +39,10 @@ def init_canvas(self, figure_size, dpi): def convert2img(self, fig, ax): ax.set_position((0, 0, 1, 1)) fig.canvas.draw() - data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) - data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) - img = Image.fromarray(data) - self._data = data + buf = BytesIO() + fig.savefig(buf, format='png') + buf.seek(0) + img = Image.open(buf) self._img = img return img From 9470b11bf2272c1e5770773ca502af70f3deb4a6 Mon Sep 17 00:00:00 2001 From: Mike Gimelfarb <35513382+mike-gimelfarb@users.noreply.github.com> Date: Thu, 2 Jan 2025 04:17:19 -0500 Subject: [PATCH 2/2] Proper matplotlib fix for 3.10 --- pyRDDLGym/core/visualizer/chart.py | 9 ++++----- pyRDDLGym/core/visualizer/heatmap.py | 9 ++++----- pyRDDLGym/core/visualizer/text.py | 9 ++++----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/pyRDDLGym/core/visualizer/chart.py b/pyRDDLGym/core/visualizer/chart.py index 16b5d587..63cdc311 100644 --- a/pyRDDLGym/core/visualizer/chart.py +++ b/pyRDDLGym/core/visualizer/chart.py @@ -1,4 +1,3 @@ -from io import BytesIO from PIL import Image import numpy as np import matplotlib.pyplot as plt @@ -137,10 +136,10 @@ def _setup(self): def convert2img(self, fig, ax): fig.canvas.draw() - buf = BytesIO() - fig.savefig(buf, format='png') - buf.seek(0) - img = Image.open(buf) + data = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8) + data = data.reshape(fig.canvas.get_width_height()[::-1] + (4,)) + data = data[:, :, :3] + img = Image.fromarray(data) self._img = img return img diff --git a/pyRDDLGym/core/visualizer/heatmap.py b/pyRDDLGym/core/visualizer/heatmap.py index 80b4af21..f624a879 100644 --- a/pyRDDLGym/core/visualizer/heatmap.py +++ b/pyRDDLGym/core/visualizer/heatmap.py @@ -1,4 +1,3 @@ -from io import BytesIO from PIL import Image import matplotlib matplotlib.use('agg') @@ -99,10 +98,10 @@ def _setup(self): def convert2img(self, fig, ax): fig.canvas.draw() - buf = BytesIO() - fig.savefig(buf, format='png') - buf.seek(0) - img = Image.open(buf) + data = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8) + data = data.reshape(fig.canvas.get_width_height()[::-1] + (4,)) + data = data[:, :, :3] + img = Image.fromarray(data) self._img = img return img diff --git a/pyRDDLGym/core/visualizer/text.py b/pyRDDLGym/core/visualizer/text.py index 774cf80f..189d8105 100644 --- a/pyRDDLGym/core/visualizer/text.py +++ b/pyRDDLGym/core/visualizer/text.py @@ -1,4 +1,3 @@ -from io import BytesIO import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt @@ -39,10 +38,10 @@ def init_canvas(self, figure_size, dpi): def convert2img(self, fig, ax): ax.set_position((0, 0, 1, 1)) fig.canvas.draw() - buf = BytesIO() - fig.savefig(buf, format='png') - buf.seek(0) - img = Image.open(buf) + data = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8) + data = data.reshape(fig.canvas.get_width_height()[::-1] + (4,)) + data = data[:, :, :3] + img = Image.fromarray(data) self._img = img return img