File tree Expand file tree Collapse file tree 2 files changed +10
-15
lines changed Expand file tree Collapse file tree 2 files changed +10
-15
lines changed Original file line number Diff line number Diff line change 3
3
Agg Buffer
4
4
==========
5
5
6
- Use backend agg to access the figure canvas as an RGB string and then
7
- convert it to an array and pass it to Pillow for rendering.
6
+ Use backend agg to access the figure canvas as an RGBA buffer, convert it to an
7
+ array, and pass it to Pillow for rendering.
8
8
"""
9
9
10
10
import numpy as np
18
18
19
19
agg = canvas .switch_backends (FigureCanvasAgg )
20
20
agg .draw ()
21
- s , (width , height ) = agg .print_to_buffer ()
22
-
23
- # Convert to a NumPy array.
24
- X = np .frombuffer (s , np .uint8 ).reshape ((height , width , 4 ))
21
+ X = np .asarray (agg .buffer_rgba ())
25
22
26
23
# Pass off to PIL.
27
24
from PIL import Image
28
- im = Image .frombytes ( "RGBA" , ( width , height ), s )
25
+ im = Image .fromarray ( X )
29
26
30
27
# Uncomment this line to display the image using ImageMagick's `display` tool.
31
28
# im.show()
Original file line number Diff line number Diff line change 37
37
# etc.).
38
38
fig .savefig ("test.png" )
39
39
40
- # Option 2: Save the figure to a string .
40
+ # Option 2: Retrieve a view on the renderer buffer.. .
41
41
canvas .draw ()
42
- s , (width , height ) = canvas .print_to_buffer ()
43
-
44
- # Option 2a: Convert to a NumPy array.
45
- X = np .frombuffer (s , np .uint8 ).reshape ((height , width , 4 ))
46
-
47
- # Option 2b: Pass off to PIL.
42
+ buf = canvas .buffer_rgba ()
43
+ # ... convert to a NumPy array ...
44
+ X = np .asarray (buf )
45
+ # ... and pass it to PIL.
48
46
from PIL import Image
49
- im = Image .frombytes ( "RGBA" , ( width , height ), s )
47
+ im = Image .fromarray ( X )
50
48
51
49
# Uncomment this line to display the image using ImageMagick's `display` tool.
52
50
# im.show()
You can’t perform that action at this time.
0 commit comments