Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d11bf4

Browse files
committed
added simple watermark examples
svn path=/trunk/matplotlib/; revision=5669
1 parent 11dab91 commit 4d11bf4

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

examples/api/watermark_image.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Use a PNG file as a watermark
3+
"""
4+
import numpy as np
5+
import matplotlib
6+
matplotlib.use('Agg')
7+
8+
import matplotlib.image as image
9+
import matplotlib.pyplot as plt
10+
11+
im = image.imread('../data/logo2.png')
12+
im[:,:,-1] = 0.5 # set the alpha channel
13+
14+
fig = plt.figure()
15+
ax = fig.add_subplot(111)
16+
17+
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')
18+
ax.grid()
19+
fig.figimage(im, 10, 10)
20+
21+
fig.savefig('watermarked', transparent=True)
22+

examples/api/watermark_text.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Use a PNG file as a watermark
3+
"""
4+
import numpy as np
5+
import matplotlib
6+
matplotlib.use('Agg')
7+
8+
import matplotlib.mathtext as mathtext
9+
import matplotlib.pyplot as plt
10+
import matplotlib
11+
matplotlib.rc('image', origin='upper')
12+
13+
dpi = 100 # save dpi
14+
w, h = 8, 6 # inches
15+
16+
parser = mathtext.MathTextParser("Bitmap")
17+
18+
rgba, depth1 = parser.to_rgba(r'Property of MPL', color='gray',
19+
fontsize=30, dpi=200)
20+
rgba[:,:,-1] *= 0.5
21+
fig = plt.figure(figsize=(w,h))
22+
23+
ax = fig.add_subplot(111)
24+
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')
25+
ax.grid()
26+
27+
imh, imw, tmp = rgba.shape
28+
29+
# position image at bottom right
30+
fig.figimage(rgba.astype(float)/255., w*dpi-imw, 0)
31+
32+
33+
fig.savefig('watermarked_text', transparent=True, dpi=dpi)
34+

examples/data/logo2.png

32.8 KB
Loading

0 commit comments

Comments
 (0)