|
|
|||||||||||||||||||||||||||||
|
pylab_examples_Examples 02_agg_buffer. |
H.Kamifuji . |
バックエンド agg を使用して Figure キャンバスに RGB 文字列としてアクセスし、次にそれを配列に変換してレンダリングのためにピローに渡します。
"""
Use backend agg to access the figure canvas as an RGB string and then
convert it to an array and pass it to Pillow for rendering.
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
try:
from PIL import Image
except ImportError:
raise SystemExit("Pillow must be installed to run this example")
plt.plot([1, 2, 3])
canvas = plt.get_current_fig_manager().canvas
agg = canvas.switch_backends(FigureCanvasAgg)
agg.draw()
s = agg.tostring_rgb()
# get the width and the height to resize the matrix
l, b, w, h = agg.figure.bbox.bounds
w, h = int(w), int(h)
X = np.fromstring(s, np.uint8)
X.shape = h, w, 3
try:
im = Image.fromstring("RGB", (w, h), s)
except Exception:
im = Image.frombytes("RGB", (w, h), s)
# Uncomment this line to display the image using ImageMagick's
# `display` tool.
# im.show()
|
![]() Python 3.11.6 (matplotlib 3.7.1) では、下記のような警告があるが、実行できる。
M:\______\agg_buffer.py:29: DeprecationWarning:
The binary mode of fromstring is deprecated, as it behaves surprisingly
on unicode inputs. Use frombuffer instead
X = np.fromstring(s, np.uint8)
Python 3.12.0 (matplotlib 3.8.1) では、下記のような警告があるが、実行できる。
E:\______\agg_buffer.py:21: MatplotlibDeprecationWarning:
The switch_backends function was deprecated in Matplotlib 3.8 and will be removed
two minor releases later.
agg = canvas.switch_backends(FigureCanvasAgg)
E:\______\agg_buffer.py:23: MatplotlibDeprecationWarning:
The tostring_rgb function was deprecated in Matplotlib 3.8 and will be removed
two minor releases later. Use buffer_rgba instead.
s = agg.tostring_rgb()
E:\______\agg_buffer.py:29: DeprecationWarning:
The binary mode of fromstring is deprecated, as it behaves surprisingly on
unicode inputs. Use frombuffer instead
X = np.fromstring(s, np.uint8)
代替サンプルは、見つからない。 |
|
pylab_examples_Examples code: agg_buffer.py |
|