Skip to content

Commit 2e6ace0

Browse files
committed
fix: fixed plt_to_np method
1 parent d078a17 commit 2e6ace0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

ratio1/_ver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__VER__ = "3.4.125"
1+
__VER__ = "3.4.126"
22

33
if __name__ == "__main__":
44
with open("pyproject.toml", "rt") as fd:

ratio1/logging/logger_mixins/computer_vision_mixin.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,18 @@ def plt_to_np(plt, close=True, axis=False):
270270
plt.axis('off')
271271
fig = plt.gcf()
272272
fig.canvas.draw()
273-
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
274-
w, h = fig.canvas.get_width_height()
275273
try:
276-
np_img = data.reshape((int(h), int(w), -1))
274+
w, h = fig.canvas.get_width_height()
275+
if hasattr(fig.canvas, 'tostring_rgb'):
276+
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
277+
np_img = data.reshape((int(h), int(w), 3))
278+
elif hasattr(fig.canvas, 'tostring_argb'):
279+
data = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
280+
argb = data.reshape((int(h), int(w), 4))
281+
np_img = argb[:, :, 1:4]
282+
else:
283+
rgba = np.asarray(fig.canvas.buffer_rgba())
284+
np_img = rgba[:, :, :3]
277285
except Exception as e:
278286
print(e)
279287
np_img = None
@@ -440,4 +448,4 @@ def image_resize(image, width=None, height=None, inter=None):
440448
resized = cv2.resize(image, dim, interpolation = inter)
441449

442450
# return the resized image
443-
return resized
451+
return resized

0 commit comments

Comments
 (0)