-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathgraphics.html
More file actions
273 lines (201 loc) · 16.8 KB
/
Copy pathgraphics.html
File metadata and controls
273 lines (201 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>第 20 章 图形开发</title><link rel="stylesheet" type="text/css" href="docbook.css" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="php,pear,pecl,phar, python, , " /><link rel="home" href="index.html" title="Netkiller Python 手札" /><link rel="up" href="index.html" title="Netkiller Python 手札" /><link rel="prev" href="LangChain/ch19s04.html" title="19.4. 相似度搜索" /><link rel="next" href="graphviz.html" title="20.2. graphviz" /></head><body><a xmlns="" href="//www.netkiller.cn/">Home</a> | <a xmlns="" href="//netkiller.github.io/">简体中文</a> | <a xmlns="" href="http://netkiller.sourceforge.net/">繁体中文</a> | <a xmlns="" href="/journal/index.html">杂文</a>
| <a xmlns="" href="https://github.com/netkiller">Github</a> | <a xmlns="" href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a> | <a xmlns="" href="https://www.facebook.com/bg7nyt">Facebook</a> | <a xmlns="" href="http://cn.linkedin.com/in/netkiller/">Linkedin</a> | <a xmlns="" href="https://www.youtube.com/user/bg7nyt/videos">Youtube</a> | <a xmlns="" href="//www.netkiller.cn/home/donations.html">打赏(Donations)</a> | <a xmlns="" href="//www.netkiller.cn/home/about.html">About</a><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">第 20 章 图形开发</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="LangChain/ch19s04.html">上一页</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="graphviz.html">下一页</a></td></tr></table><hr /></div><table xmlns=""><tr><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=watch&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=fork&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&type=follow&count=true&size=large" height="30" width="240" frameborder="0" scrolling="0" style="width:240px; height: 30px;" allowTransparency="true"></iframe></td><td></td><td><a href="https://zhuanlan.zhihu.com/netkiller"><img src="/images/logo/zhihu-card-default.svg" height="25" /></a></td><td valign="middle"><a href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a></td><td></td><td></td><td></td><td></td></tr></table><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a id="graphics"></a>第 20 章 图形开发</h1></div></div></div><div class="toc"><p><strong>目录</strong></p><dl class="toc"><dt><span class="section"><a href="graphics.html#qrcode">20.1. 二维码</a></span></dt><dd><dl><dt><span class="section"><a href="graphics.html#id1237">20.1.1. qrcode</a></span></dt><dt><span class="section"><a href="graphics.html#MyQR">20.1.2. MyQR</a></span></dt><dt><span class="section"><a href="graphics.html#id1238">20.1.3. 从图片识别二维码</a></span></dt><dt><span class="section"><a href="graphics.html#id1239">20.1.4. 从摄像头识别二维码</a></span></dt></dl></dd><dt><span class="section"><a href="graphviz.html">20.2. graphviz</a></span></dt><dd><dl><dt><span class="section"><a href="graphviz.html#id1240">20.2.1. 安装 graphviz 环境</a></span></dt><dt><span class="section"><a href="graphviz.html#id1241">20.2.2. 例子</a></span></dt></dl></dd><dt><span class="section"><a href="pyro.html">20.3. Pyro - Pyro is short for PYthon Remote Objects</a></span></dt><dt><span class="section"><a href="python-imaging.html">20.4. Python Imaging Library</a></span></dt><dt><span class="section"><a href="opencv/index.html">20.5. OpenCV</a></span></dt><dd><dl><dt><span class="section"><a href="opencv/index.html#id1242">20.5.1. 安装 OpenCV</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1243">20.5.2. 显示图片</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1244">20.5.3. 将BGR图像转为RGB</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1245">20.5.4. uint8 格式</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1246">20.5.5. 应用Canny边缘检测</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1247">20.5.6. 摄像头捕捉图像</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1248">20.5.7. imread()</a></span></dt><dt><span class="section"><a href="opencv/index.html#opencv.channel]">20.5.8. 通道拆分/合并</a></span></dt><dt><span class="section"><a href="opencv/index.html#opencv.hsv">20.5.9. HSV 色彩识别</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1249">20.5.10. 色阶调整</a></span></dt><dt><span class="section"><a href="opencv/index.html#id1250">20.5.11. 亮度调节</a></span></dt></dl></dd><dt><span class="section"><a href="pillow/index.html">20.6. PIL(Python Imaging Library)Python 图像处理库</a></span></dt><dd><dl><dt><span class="section"><a href="pillow/index.html#pillow.setup">20.6.1. 安装</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.resize">20.6.2. 缩放图像与尺寸修改</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.paste">20.6.3. 叠加图像</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.rotate">20.6.4. 图像旋转</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.width.height">20.6.5. 获取图片信息</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.new">20.6.6. 创建空图像</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.crop">20.6.7. 裁剪</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.convert">20.6.8. 彩色转灰度</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.merge">20.6.9. RGB 通道操作</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.pixel">20.6.10. 像素操作</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.text">20.6.11. 图像上加文字</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.point">20.6.12. 明暗调整</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.fromarray">20.6.13. PIL 与 Numpy 相互转换</a></span></dt><dt><span class="section"><a href="pillow/index.html#image.diff">20.6.14. 比较两张图片</a></span></dt></dl></dd><dt><span class="section"><a href="ch20s07.html">20.7. SVG 图形库</a></span></dt><dd><dl><dt><span class="section"><a href="ch20s07.html#svg.tag">20.7.1. SVG 标签</a></span></dt><dt><span class="section"><a href="ch20s07.html#drawsvg">20.7.2. drawsvg</a></span></dt><dt><span class="section"><a href="ch20s07.html#CairoSVG">20.7.3. CairoSVG - SVG 转换为 PNG/PDF/PS</a></span></dt></dl></dd><dt><span class="section"><a href="netkiller-chart.html">20.8. Netkiller SVG 图形库</a></span></dt><dd><dl><dt><span class="section"><a href="netkiller-chart.html#id1269">20.8.1. 安装</a></span></dt><dt><span class="section"><a href="netkiller-chart.html#id1285">20.8.2. SVG 图形库</a></span></dt><dt><span class="section"><a href="netkiller-chart.html#id1288">20.8.3. Gantt 甘特图</a></span></dt><dt><span class="section"><a href="netkiller-chart.html#id1289">20.8.4. 工作负载图</a></span></dt><dt><span class="section"><a href="netkiller-chart.html#id1290">20.8.5. 思维导图</a></span></dt><dt><span class="section"><a href="netkiller-chart.html#id1291">20.8.6. 石川鱼骨图</a></span></dt></dl></dd></dl></div>
<div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="qrcode"></a>20.1. 二维码</h2></div></div></div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="id1237"></a>20.1.1. qrcode</h3></div></div></div>
<p>安装</p>
<p>pip install qrcode</p>
<pre class="screen">
neo@MacBook-Pro-Neo ~ % pip install qrcode
Collecting qrcode
Downloading qrcode-6.1-py2.py3-none-any.whl (31 kB)
Requirement already satisfied: six in /usr/local/lib/python3.9/site-packages (from qrcode) (1.15.0)
Installing collected packages: qrcode
Successfully installed qrcode-6.1
</pre>
<p>常规二维码</p>
<pre class="programlisting">
import qrcode
img = qrcode.make('《Netkiller Python 手札》')
img.save("text.png")
img = qrcode.make('http://www.netkiller.cn')
img.save("url.png")
</pre>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1235"></a>20.1.1.1. 设置颜色</h4></div></div></div>
<p>颜色设置背景为白色,前景为绿色</p>
<pre class="programlisting">
import qrcode as qrcode
qr = qrcode.QRCode(version=1, box_size=10, border=2)
# 向二维码添加数据
qr.add_data("http://www.netkiller.cn")
qr.make(fit=True)
# 更改QR的背景为白色和绘画颜色为绿色
img = qr.make_image(fill_color="green", back_color="white")
img.save('green.png')
img.show() # 显示二维码
</pre>
<p>QRCode 参数</p>
<div class="literallayout"><p><br />
<br />
version: 范围为1到40整数(最小值是1,表示12×12的矩阵),如果想让程序自动生成,将值设置为 None 并使用 fit=True 参数即可。<br />
error_correction: 二维码的纠错范围,可以选择4个常量:<br />
1. ERROR_CORRECT_L 7%以下的错误会被纠正<br />
2. ERROR_CORRECT_M (default) 15%以下的错误会被纠正<br />
3. ERROR_CORRECT_Q 25 %以下的错误会被纠正<br />
4. ERROR_CORRECT_H. 30%以下的错误会被纠正<br />
boxsize: 每个点(方块)中的像素个数<br />
border: 二维码图像外围边框宽度,默认为4<br />
<br />
</p></div>
</div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1236"></a>20.1.1.2. qr - script to create QR codes at the command line</h4></div></div></div>
<pre class="screen">
NAME
qr - script to create QR codes at the command line
SYNOPSIS
qr [--help] [--factory=FACTORY] [--optimize=OPTIMIZE] [--error-correction=LEVEL]
[data]
DESCRIPTION
This script uses the python qrcode module. It can take data from stdin or from the
commandline and generate a QR code. Normally it will output the QR code as ascii
art to the terminal. If the output is piped to a file, it will output the image
(default type of PNG).
OPTIONS
-h, --help
Show a help message.
--factory=FACTORY
Full python path to the image factory class to create the image with. You can
use the following shortcuts to the built-in image factory classes: pil
(default), pymaging, svg, svg-fragment, svg-path.
--optimize=OPTIMIZE
Optimize the data by looking for chunks of at least this many characters that
could use a more efficient encoding method. Use 0 to turn off chunk optimiza-
tion.
--error-correction=LEVEL
The error correction level to use. Choices are L (7%), M (15%, default), Q
(25%), and H (30%).
data
The data from which the QR code will be generated.
SEE ALSO
https://github.com/lincolnloop/python-qrcode/
</pre>
<p>使用方法</p>
<pre class="screen">
neo@MacBook-Pro-Neo ~ % qr "Some text" > test.png
</pre>
</div>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="MyQR"></a>20.1.2. MyQR</h3></div></div></div>
<p>安装依赖包</p>
<pre class="screen">
pip install myqr
</pre>
<pre class="programlisting">
from MyQR import myqr
# myqr.run 参数:
# words:文本/链接,或者你想说的话(不支持中文,很不友好)
# picture:背景图
# colorsize:True 表示生成彩图
# save_name:生成的二维码文件名
myqr.run(words="http://www.netkiller.com",
picture="db.png", colorized=True,
save_name="netkiller.png")
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="id1238"></a>20.1.3. 从图片识别二维码</h3></div></div></div>
<p>安装依赖包</p>
<pre class="screen">
pip install pyzbar
</pre>
<p>pyzbar 是调用 zbar 共享库,所以还需要安装 zbar</p>
<pre class="screen">
brew install zbar
</pre>
<pre class="programlisting">
import numpy as np
from PIL import Image
import pyzbar.pyzbar as pyzbar
# 读取文件,转成数组
im = np.array(Image.open("netkiller.png"))
print(pyzbar.decode(im))
# 返回的信息
print('-' * 50)
# 读取内容
print(pyzbar.decode(im)[0].data.decode("utf-8"))
</pre>
<p>输出信息</p>
<pre class="screen">
[Decoded(data=b'http://www.netkiller.com', type='QRCODE', rect=Rect(left=36, top=36, width=297, height=297), polygon=[Point(x=36, y=36), Point(x=36, y=332), Point(x=333, y=333), Point(x=332, y=36)])]
--------------------------------------------------
http://www.netkiller.com
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="id1239"></a>20.1.4. 从摄像头识别二维码</h3></div></div></div>
<pre class="programlisting">
import cv2
import pyzbar.pyzbar as pyzbar
def decodeQrcode(image):
barcodes = pyzbar.decode(image)
for barcode in barcodes:
# 提取二维码数据
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# 打印调试信息
print("[INFO] Found {}: {}".format(barcodeType, barcodeData))
# 取出二维码在图像中的位置
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h),
(255, 255, 255), cv2.BORDER_DEFAULT)
# 框出图像中的二维码
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
.5, (225, 225, 225), 2)
return image
if __name__ == '__main__':
# 默认摄像头是0,如果读取不到,改为1试试
camera = cv2.VideoCapture(1)
while True:
# 从摄像头读取当前帧
ret, frame = camera.read()
# 转为灰度图像,转递给解码函数
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
im = decodeQrcode(gray)
cv2.waitKey(5)
cv2.imshow("camera", im)
# 设置退出按键,按下q跳出本次循环
if cv2.waitKey(5) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
</pre>
</div>
</div>
</div><script xmlns="" type="text/javascript" id="clustrmaps" src="//cdn.clustrmaps.com/map_v2.js?u=r5HG&d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM"></script><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="LangChain/ch19s04.html">上一页</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="graphviz.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">19.4. 相似度搜索 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 20.2. graphviz</td></tr></table></div><script xmlns="">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11694057-1', 'auto');
ga('send', 'pageview');
</script><script xmlns="" async="async">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script xmlns="" async="async">
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script></body></html>