{"id":204019,"date":"2025-05-29T14:07:23","date_gmt":"2025-05-29T06:07:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204019\/"},"modified":"2025-05-29T14:07:23","modified_gmt":"2025-05-29T06:07:23","slug":"%e6%88%91%e5%a6%82%e4%bd%95%e7%94%a8-python-%e5%88%9b%e5%bb%ba-qr-%e7%a0%81%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204019\/","title":{"rendered":"\u6211\u5982\u4f55\u7528 Python \u521b\u5efa QR \u7801\u751f\u6210\u5668"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6211\u5982\u4f55\u7528 Python \u521b\u5efa QR \u7801\u751f\u6210\u5668<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6211\u5982\u4f55\u7528 Python \u521b\u5efa QR \u7801\u751f\u6210\u5668\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241019\/172931386167133c4559939.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u5c06\u662f\u4e00\u7bc7\u7b80\u77ed\u7684\u6587\u7ae0\uff0c\u4ecb\u7ecd\u6211\u5982\u4f55\u4f7f\u7528 python \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u7ef4\u7801\u751f\u6210\u5668<\/p>\n<p>\u5bf9\u4e8e\u6b64\u6b65\u9aa4\uff0c\u60a8\u9700\u8981\u4f7f\u7528 qrcode \u5e93\uff1ahttps:\/\/pypi.org\/project\/qrcode\/<\/p>\n<p>\u521b\u5efa\u9879\u76ee\u6587\u4ef6\u5939\u540e\u6211\u6240\u505a\u7684\u7b2c\u4e00\u6b65\u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u3002 python \u4e2d\u7684\u865a\u62df\u73af\u5883\u53ea\u662f\u8ba1\u7b97\u673a\u4e0a\u53e6\u4e00\u4e2a\u72ec\u7acb\u7684\u5de5\u4f5c\u533a\uff0c\u60a8\u53ef\u4ee5\u5728\u5176\u4e2d\u5b89\u88c5\u5305\u6765\u8fd0\u884c python \u9879\u76ee\u3002<\/p>\n<p>\u56e0\u4e3a\u6211\u4f7f\u7528\u7684\u662f mac\uff0c\u6240\u4ee5\u547d\u4ee4\u662f<\/p>\n<pre>python3 -m venv venv\n<\/pre>\n<p>\u4e0b\u4e00\u6b65\u662f\u6fc0\u6d3b\u865a\u62df\u673a<\/p>\n<pre>source venv\/bin\/activate\n<\/pre>\n<p>\u8981\u505c\u7528\u865a\u62df\u73af\u5883\uff0c\u60a8\u9700\u8981\u8f93\u5165\uff1a<\/p>\n<pre>deactivate\n<\/pre>\n<p>\u4e0b\u4e00\u6b65\u662f\u5b89\u88c5\u4e8c\u7ef4\u7801\u5305<\/p>\n<pre>pip install qrcode\n<\/pre>\n<p>\u5728\u60a8\u7684 python \u6587\u4ef6\u4e2d\uff0c\u786e\u4fdd\u5bfc\u5165 qrcode \u6a21\u5757<\/p>\n<pre>import qrcode\n<\/pre>\n<p>\u5728\u6211\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u521b\u5efa\u4e86\u4e24\u4e2a\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u540d\u4e3a data \u548c filename \u7684\u53d8\u91cf\u4e2d\u3002 strip() \u65b9\u6cd5\u5220\u9664\u5b57\u7b26\u4e32\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u7a7a\u683c\uff1a<\/p>\n<pre>data = input('enter a text or url ').strip()\nfilename = input('enter the filename ').strip()\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u8fdb\u5165 qr \u6a21\u5757\u5e76\u521b\u5efa qr \u7801\u5bf9\u8c61<\/p>\n<pre>qr = qrcode.QRCode(box_size=10, border=4)\n\nqr.add.data(data)\n\nimage = qr.make_image(fill_color = 'black', back_color = 'white')\n\nimage.save(filename)\n\nprint(f'QR Code saved as {filename}')\n<\/pre>\n<p>\u60a8\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e0a\u8fd0\u884c\u6b64\u4ee3\u7801\uff0c\u5b83\u5c06\u4f7f\u7528\u60a8\u9009\u62e9\u7684\u4efb\u4f55 url \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u7801<\/p>\n<p>\u66f4\u591a\u6587\u7ae0\u656c\u8bf7\u5173\u6ce8\uff01<br \/> \u8bf7\u5173\u6ce8 twitter(x) @abeck617 <\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u6211\u5982\u4f55\u7528 Python \u521b\u5efa QR \u7801\u751f\u6210\u5668\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u5982\u4f55\u7528 Python \u521b\u5efa Q&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204019","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204019"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204019\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}