{"id":257,"date":"2021-08-08T01:21:00","date_gmt":"2021-08-08T01:21:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=257"},"modified":"2021-08-08T01:21:00","modified_gmt":"2021-08-08T01:21:00","slug":"257","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/08\/257\/","title":{"rendered":"\u7eafpython\u4e8c\u7ef4\u7801\u751f\u6210\u5668 (google\u7ffb\u8bd1) Pure python QR Code generator"},"content":{"rendered":"<h1><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3526158478.png\" alt=\"qrcode.png\" title=\"qrcode.png\"><\/h1>\n<h1>\u7eafpython\u4e8c\u7ef4\u7801\u751f\u6210\u5668  <a href=\"https:\/\/github.com\/lincolnloop\/python-qrcode\">Pure python QR Code generator<\/a><\/h1>\n<p>\u751f\u6210\u4e8c\u7ef4\u7801\u3002<\/p>\n<p>\u5bf9\u4e8e\u6807\u51c6\u5b89\u88c5\uff08\u5305\u62ec\u7528\u4e8e\u751f\u6210\u56fe\u50cf\u7684 <code>pillow<\/code>\uff09\uff0c\u8fd0\u884c\uff1a<\/p>\n<pre><code>pip install qrcode\n\n<\/code><\/pre>\n<h1>\u4ec0\u4e48\u662f\u4e8c\u7ef4\u7801\uff1f<\/h1>\n<p>\u5feb\u901f\u54cd\u5e94\u7801\u662f\u4e00\u79cd\u4e8c\u7ef4\u8c61\u5f62\u7801\uff0c\u5177\u6709\u5feb\u901f\u7684\u53ef\u8bfb\u6027\u548c\u8f83\u5927\u7684\u5b58\u50a8\u5bb9\u91cf\u3002\u8be5\u4ee3\u7801\u7531\u5728\u767d\u8272\u80cc\u666f\u4e0a\u4ee5\u65b9\u5f62\u56fe\u6848\u6392\u5217\u7684\u9ed1\u8272\u6a21\u5757\u7ec4\u6210\u3002<br \/>\u7f16\u7801\u7684\u4fe1\u606f\u53ef\u4ee5\u7531\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u7ec4\u6210\uff08\u4f8b\u5982\uff0c\u4e8c\u8fdb\u5236\u3001\u5b57\u6bcd\u6570\u5b57\u6216\u6c49\u5b57\u7b26\u53f7\uff09<\/p>\n<h1>\u7528\u6cd5<\/h1>\n<p>\u4ece\u547d\u4ee4\u884c\uff0c\u4f7f\u7528\u5df2\u5b89\u88c5\u7684 <code>qr<\/code> \u811a\u672c\uff1a<\/p>\n<pre><code>qr \"Some text\" &gt; test.png\n<\/code><\/pre>\n<p>\u6216\u8005\u5728 Python \u4e2d\uff0c\u4f7f\u7528 <code>make<\/code> \u5feb\u6377\u529f\u80fd\uff1a<\/p>\n<pre><code>import qrcode\nimg = qrcode.make('Some data here')\ntype(img)  # qrcode.image.pil.PilImage\nimg.save(\"some_file.png\")\n<\/code><\/pre>\n<h2>\u9ad8\u7ea7\u7528\u6cd5<\/h2>\n<hr \/>\n<p>\u5982\u9700\u66f4\u591a\u63a7\u5236\uff0c\u8bf7\u4f7f\u7528 <code>QRCode<\/code>\u8be5\u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>import qrcode\nqr = qrcode.QRCode(\n    version=1,\n    error_correction=qrcode.constants.ERROR_CORRECT_L,\n    box_size=10,\n    border=4,\n)\nqr.add_data('Some data')\nqr.make(fit=True)\n\nimg = qr.make_image(fill_color=\"black\", back_color=\"white\")\n\n<\/code><\/pre>\n<p>\u8be5  <code>version<\/code> \u53c2\u6570\u662f\u4e00\u4e2a 1 \u5230 40 \u4e4b\u95f4\u7684\u6574\u6570\uff0c\u7528\u4e8e\u63a7\u5236 QR \u7801\u7684\u5927\u5c0f\uff08\u6700\u5c0f\u7684\u7248\u672c 1 \u662f\u4e00\u4e2a 21x21 \u77e9\u9635\uff09\u3002<br \/>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u8bbe\u7f6e <code>None<\/code> \u5e76\u4f7f\u7528\u8be5fit\u53c2\u6570\u6765\u81ea\u52a8\u786e\u5b9a\u8fd9\u4e00\u70b9\u3002<\/p>\n<p><code>fill_color<\/code> \u548c <code>back_color<\/code> \u53ef\u4ee5\u5728\u4f7f\u7528\u9ed8\u8ba4\u56fe\u50cf\u5de5\u5382\u65f6\u66f4\u6539 QR \u7684\u80cc\u666f\u548c\u7ed8\u753b\u989c\u8272\u3002\u8fd9\u4e24\u4e2a\u53c2\u6570\u90fd\u63a5\u53d7 RGB \u989c\u8272\u5143\u7ec4\u3002<\/p>\n<pre><code>img = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35))\n<\/code><\/pre>\n<p>\u8be5 <code>error_correction<\/code> \u53c2\u6570\u63a7\u5236\u7528\u4e8e\u4e8c\u7ef4\u7801\u7684\u7ea0\u9519\u3002 <code>qrcode<\/code> \u5305\u4e2d\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u56db\u4e2a\u5e38\u91cf\uff1a<\/p>\n<p><code>ERROR_CORRECT_L<\/code><\/p>\n<pre><code>\u5927\u7ea6\u53ef\u4ee5\u7ea0\u6b63 7% \u6216\u66f4\u5c11\u7684\u9519\u8bef\u3002<\/code><\/pre>\n<p><code>ERROR_CORRECT_M<\/code> (default)<\/p>\n<pre><code>\u5927\u7ea6 15% \u6216\u66f4\u5c11\u7684\u9519\u8bef\u53ef\u4ee5\u88ab\u7ea0\u6b63\u3002<\/code><\/pre>\n<p><code>ERROR_CORRECT_Q<\/code><\/p>\n<pre><code>\u5927\u7ea6 25% \u6216\u66f4\u5c11\u7684\u9519\u8bef\u53ef\u4ee5\u88ab\u7ea0\u6b63\u3002<\/code><\/pre>\n<p><code>ERROR_CORRECT_H<\/code>.<\/p>\n<pre><code>\u5927\u7ea6\u53ef\u4ee5\u7ea0\u6b63 30% \u6216\u66f4\u5c11\u7684\u9519\u8bef\u3002\n<\/code><\/pre>\n<p>\u8be5 <code>box_size<\/code>\u53c2\u6570\u63a7\u5236\u4e8c\u7ef4\u7801\u7684\u6bcf\u4e2a <code>box<\/code> \u6709\u591a\u5c11\u50cf\u7d20\u3002<\/p>\n<p>\u8be5 <code>border<\/code>\u53c2\u6570\u63a7\u5236\u8fb9\u6846\u5e94\u8be5\u6709\u591a\u5c11\u4e2a\u6846\u539a\uff08\u9ed8\u8ba4\u4e3a 4\uff0c\u8fd9\u662f\u6839\u636e\u89c4\u8303\u7684\u6700\u5c0f\u503c\uff09\u3002<\/p>\n<h1>\u5176\u4ed6\u56fe\u50cf\u5de5\u5382<\/h1>\n<p>\u60a8\u53ef\u4ee5\u7f16\u7801\u4e3a SVG\uff0c\u6216\u4f7f\u7528\u65b0\u7684\u7eaf Python \u56fe\u50cf\u5904\u7406\u5668\u7f16\u7801\u4e3a PNG \u56fe\u50cf\u3002<\/p>\n<p>\u4e0b\u9762\u7684 Python \u793a\u4f8b\u4f7f\u7528\u4e86make\u5feb\u6377\u65b9\u5f0f\u3002\u76f8\u540c\u7684image_factory \u5173\u952e\u5b57\u53c2\u6570\u662fQRCode\u7528\u4e8e\u66f4\u9ad8\u7ea7\u7528\u6cd5\u7684\u7c7b\u7684\u6709\u6548\u9009\u9879\u3002<\/p>\n<p>SVG<br \/>\u60a8\u53ef\u4ee5\u521b\u5efa\u6574\u4e2a SVG \u6216 SVG \u7247\u6bb5\u3002\u5728\u6784\u5efa\u6574\u4e2a SVG \u56fe\u50cf\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7ec4\u5408\u4f5c\u4e3a\u8def\u5f84\u7684\u5de5\u5382\uff08\u63a8\u8350\u548c\u9ed8\u8ba4\u7528\u4e8e\u811a\u672c\uff09\u6216\u521b\u5efa\u4e00\u7ec4\u7b80\u5355\u77e9\u5f62\u7684\u5de5\u5382\u3002<\/p>\n<p>\u4ece\u60a8\u7684\u547d\u4ee4\u884c\uff1a<\/p>\n<pre><code>qr --factory=svg-path \"Some text\" &gt; test.svg\nqr --factory=svg \"Some text\" &gt; test.svg\nqr --factory=svg-fragment \"Some text\" &gt; test.svg\n<\/code><\/pre>\n<p>\u6216\u8005\u5728 Python \u4e2d\uff1a<\/p>\n<pre><code>import qrcode\nimport qrcode.image.svg\n\nif method == 'basic':\n    # Simple factory, just a set of rects.\n    factory = qrcode.image.svg.SvgImage\nelif method == 'fragment':\n    # Fragment factory (also just a set of rects)\n    factory = qrcode.image.svg.SvgFragmentImage\nelse:\n    # Combined path factory, fixes white space that may occur when zooming\n    factory = qrcode.image.svg.SvgPathImage\n\nimg = qrcode.make('Some data here', image_factory=factory)\n<\/code><\/pre>\n<p>\u5176\u4ed6\u4e24\u4e2a\u76f8\u5173\u7684\u5de5\u5382\u53ef\u7528\uff0c\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\uff0c\u4f46\u4e5f\u7528\u767d\u8272\u586b\u5145 SVG \u7684\u80cc\u666f\uff1a<\/p>\n<pre><code>qrcode.image.svg.SvgFillImage\nqrcode.image.svg.SvgPathFillImage\n\n<\/code><\/pre>\n<h2>Pure Python PNG<\/h2>\n<p>\u5b89\u88c5\u4ee5\u4e0b\u4e24\u4e2a\u5305\uff1a<\/p>\n<pre><code>pip install -e git+git:\/\/github.com\/ojii\/pymaging.git#egg=pymaging\npip install -e git+git:\/\/github.com\/ojii\/pymaging-png.git#egg=pymaging-png\n<\/code><\/pre>\n<p>\u4ece\u60a8\u7684\u547d\u4ee4\u884c\uff1a<\/p>\n<pre><code>qr --factory=pymaging \"Some text\" &gt; test.png\n<\/code><\/pre>\n<p>\u6216\u8005\u5728 Python \u4e2d\uff1a<\/p>\n<pre><code>import qrcode\nfrom qrcode.image.pure import PymagingImage\nimg = qrcode.make('Some data here', image_factory=PymagingImage)\n\n<\/code><\/pre>\n<h2>\u6837\u5f0f\u56fe\u50cf<\/h2>\n<p>\u8981\u5c06\u6837\u5f0f\u5e94\u7528\u4e8e QRCode\uff0c\u8bf7\u4f7f\u7528 StyledPilImage \u56fe\u50cf\u5de5\u5382\u3002\u8fd9\u9700\u8981\u4e00\u4e2a\u53ef\u9009\u7684\u6a21\u5757\u62bd\u5c49\u6765\u63a7\u5236\u4e8c\u7ef4\u7801\u7684\u5f62\u72b6\uff0c<br \/>\u4e00\u4e2a\u53ef\u9009\u7684\u989c\u8272\u906e\u7f69\u6765\u6539\u53d8\u4e8c\u7ef4\u7801\u7684\u989c\u8272\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53ef\u9009\u7684\u56fe\u50cf\u5d4c\u5165\u4e2d\u5fc3\u3002<\/p>\n<p>\u8fd9\u4e9b QR \u7801\u4e0d\u80fd\u4fdd\u8bc1\u9002\u7528\u4e8e\u6240\u6709\u9605\u8bfb\u5668\uff0c\u56e0\u6b64\u8bf7\u8fdb\u884c\u4e00\u4e9b\u5b9e\u9a8c\u5e76\u5c06\u7ea0\u9519\u8bbe\u7f6e\u4e3a\u9ad8\uff08\u5c24\u5176\u662f\u5728\u5d4c\u5165\u56fe\u50cf\u65f6\uff09\u3002<\/p>\n<p>\u7ed8\u5236\u5e26\u6709\u5706\u89d2\u3001\u5f84\u5411\u6e10\u53d8\u548c\u5d4c\u5165\u56fe\u50cf\u7684 QR \u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import qrcode\nfrom qrcode.image.styledpil import StyledPilImage\nfrom qrcode.image.styles.moduledrawers import RoundedModuleDrawer\nfrom qrcode.image.styles.colormasks import RadialGradiantColorMask\n\nqr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)\nqr.add_data('Some data')\n\nimg_1 = qr.make_image(image_factory=StyledPilImage, module_drawer=RoundedModuleDrawer())\nimg_2 = qr.make_image(image_factory=StyledPilImage, color_mask=RadialGradiantColorMask())\nimg_3 = qr.make_image(image_factory=StyledPilImage, embeded_image_path=\"\/path\/to\/image.png\")\n<\/code><\/pre>\n<p>\u4e8c\u7ef4\u7801\u6837\u5f0f:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/4287813398.png\" alt=\"module_drawers.png\" title=\"module_drawers.png\"><\/p>\n<p>\u4e8c\u7ef4\u7801\u989c\u8272\u586b\u5145:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/46104156.png\" alt=\"color_masks.png\" title=\"color_masks.png\"><\/p>\n<h1>\u4f8b\u5b50<\/h1>\n<p>\u4ece  <code>print_ascii<\/code> \u83b7\u53d6\u6587\u672c\u5185\u5bb9:<\/p>\n<pre><code>import io\nimport qrcode\nqr = qrcode.QRCode()\nqr.add_data(\"Some text\")\nf = io.StringIO()\nqr.print_ascii(out=f)\nf.seek(0)\nprint(f.read())\n<\/code><\/pre>\n<p><code>add_data<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u9644\u52a0\u5230\u5f53\u524d QR \u5bf9\u8c61\u3002\u8981\u901a\u8fc7\u66ff\u6362\u540c\u4e00\u5bf9\u8c61\u4e2d\u4ee5\u524d\u7684\u5185\u5bb9\u6765\u6dfb\u52a0\u65b0\u6570\u636e\uff0c\u8bf7\u9996\u5148\u4f7f\u7528 <code>clear<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre><code>import qrcode\nqr = qrcode.QRCode()\nqr.add_data('Some data')\nimg = qr.make_image()\nqr.clear()\nqr.add_data('New data')\nother_img = qr.make_image()\n<\/code><\/pre>\n<p>\u7ba1\u9053\u8f93\u51fa\u5230\u547d\u4ee4\u884c\u4e2d\u7684 <code>ascii<\/code> \u6587\u672c\u6587\u4ef6\uff1a<\/p>\n<pre><code>qr --ascii \"Some data\" &gt; \"test.txt\"\ncat test.txt\n<\/code><\/pre>\n<p>\u66ff\u4ee3\u7ba1\u9053\u8f93\u51fa\u5230\u6587\u4ef6\u4ee5\u907f\u514d PoweShell \u95ee\u9898\uff1a<\/p>\n<pre><code># qr \"Some data\" &gt; test.png\nqr --output=test.png \"Some data\"\n<\/code><\/pre>\n<p>\u7eafpython\u4e8c\u7ef4\u7801\u751f\u6210\u5668  <a href=\"https:\/\/github.com\/lincolnloop\/python-qrcode\">Pure python QR Code generator<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7eafpython\u4e8c\u7ef4\u7801\u751f\u6210\u5668 Pure python QR Code generator \u751f\u6210\u4e8c\u7ef4\u7801\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}