Skip to content

Commit 0cce33a

Browse files
committed
add imgcheck_demo.py
1 parent 6159d81 commit 0cce33a

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

test/imgcheck_demo.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import os
2+
3+
4+
if True:
5+
outdir = os.path.dirname(os.path.realpath(__file__))
6+
else:
7+
# Put images in dir with name that depends on mpl version.
8+
from matplotlib import __version__ as matplotlib_version
9+
matplotlib_version = ".".join(matplotlib_version.split(".")[0:2])
10+
fdir = os.path.dirname(os.path.realpath(__file__))
11+
outdir = os.path.join(fdir, "imagecheck", "mpl-" + matplotlib_version)
12+
os.makedirs(outdir)
13+
14+
from matplotlib import rc_context
15+
from matplotlib import rcParams
16+
17+
opts = {
18+
'rcParams':
19+
{
20+
'savefig.dpi': 144,
21+
'savefig.format': 'png',
22+
'savefig.bbox': 'tight',
23+
'savefig.transparent': False,
24+
'figure.max_open_warning': 50,
25+
'figure.figsize': (7, 3),
26+
'figure.dpi': 144,
27+
'axes.titlesize': 10,
28+
"font.family": "serif",
29+
"font.serif": rcParams['font.serif'],
30+
"font.weight": "normal"
31+
},
32+
'_rcParams': {
33+
'figure.bbox': 'tight'
34+
}
35+
}
36+
37+
if True:
38+
ref_file = os.path.join(outdir, "imgcheck_demo.ref.png")
39+
now_file = os.path.join(outdir, "imgcheck_demo.now.png")
40+
41+
if os.path.exists(ref_file):
42+
os.remove(ref_file)
43+
if os.path.exists(now_file):
44+
os.remove(now_file)
45+
46+
# Create ref image
47+
from matplotlib import pyplot as plt
48+
fig, ax = plt.subplots()
49+
ax.plot(0.25,0.25,"*")
50+
ax.set_xlim([0,1])
51+
ax.set_ylim([0,1])
52+
53+
with rc_context(rc=opts['rcParams']):
54+
fig.savefig(ref_file)
55+
fig.savefig(now_file)
56+
57+
from imgcheck import imgcheck
58+
59+
# Should be "PASS"
60+
imgcheck(ref_file, now_file)
61+
62+
# Create a file that differs from ref_file
63+
from matplotlib import pyplot as plt
64+
fig, ax = plt.subplots()
65+
ax.plot(0.5,0.5,"o")
66+
ax.set_xlim([0,1])
67+
ax.set_ylim([0,1])
68+
69+
with rc_context(rc=opts['rcParams']):
70+
fig.savefig(now_file)
71+
72+
# Should be "FAIL"
73+
imgcheck(ref_file, now_file)
74+
75+
76+
if True:
77+
78+
# Create ref image
79+
from matplotlib import pyplot as plt
80+
fig, ax = plt.subplots()
81+
ax.plot(0.25,0.25,"*")
82+
ax.set_xlim([0,1])
83+
ax.set_ylim([0,1])
84+
85+
if opts['_rcParams']['figure.bbox'] == 'tight':
86+
fig.tight_layout()
87+
88+
from io import BytesIO
89+
ref_bytes = BytesIO()
90+
fig.canvas.print_figure(ref_bytes)
91+
ref_bytes = ref_bytes.getvalue()
92+
with open(ref_file,'wb') as f:
93+
f.write(ref_bytes)
94+
95+
# Pass filename and bytes
96+
imgcheck(ref_file, ref_bytes)
97+
98+
# Create a figure that differs from ref_file
99+
from matplotlib import pyplot as plt
100+
fig, ax = plt.subplots()
101+
ax.plot(0.5,0.5,"o")
102+
ax.set_xlim([0,1])
103+
ax.set_ylim([0,1])
104+
105+
now_bytes = BytesIO()
106+
fig.canvas.print_figure(now_bytes)
107+
now_bytes = now_bytes.getvalue()
108+
109+
# Pass filename and bytes
110+
imgcheck(ref_file, now_bytes)

0 commit comments

Comments
 (0)