日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python二维码生成库qrcode示例

發布時間:2023/12/15 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python二维码生成库qrcode示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二維碼簡稱 QR Code(Quick Response Code),學名為快速響應矩陣碼,是二維條碼的一種,由日本的 Denso Wave 公司于 1994 年發明。現隨著智能手機的普及,已廣泛應用于平常生活中,例如商品信息查詢、社交好友互動、網絡地址訪問等等。

由于生成 qrcode 圖片需要依賴 Python 的圖像庫,所以需要先安裝 Python 圖像庫 PIL(Python Imaging Library),不然會遇到 ImportError: No module named Image的錯誤。

PNG

From the command line, use the installed qr script:

qr "Some text" > test.png

Example:

import qrcodeqr = qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=4, ) qr.add_data('http://zzir.cn/') qr.make(fit=True) img = qr.make_image() img.save("qrcode_demo.png")

參數 version 表示生成二維碼的尺寸大小,取值范圍是 1 至 40,最小尺寸 1 會生成 21 * 21 的二維碼,version 每增加 1,生成的二維碼就會添加 4 尺寸,例如 version 是 2,則生成 25 * 25 的二維碼。

參數 error_correction 指定二維碼的容錯系數,分別有以下4個系數:

  • ERROR_CORRECT_L: 7%的字碼可被容錯

  • ERROR_CORRECT_M: 15%的字碼可被容錯

  • ERROR_CORRECT_Q: 25%的字碼可被容錯

  • ERROR_CORRECT_H: 30%的字碼可被容錯

  • 參數 box_size 表示二維碼里每個格子的像素大小。

    參數 border 表示邊框的格子厚度是多少(默認是4)。

    SVG

    On Python 2.6 must install lxml since the older xml.etree.ElementTree version can not be used to create SVG images.

    You can create the entire SVG or an SVG fragment. When building an entire SVG image, you can use the factory that combines as a path (recommended, and default for the script) or a factory that creates a simple set of rectangles.

    From your command line:

    qr --factory=svg-path "Some text" > test.svg qr --factory=svg "Some text" > test.svg qr --factory=svg-fragment "Some text" > test.svg

    Or in Python:

    import qrcode import qrcode.image.svgif method == 'basic':# Simple factory, just a set of rects.factory = qrcode.image.svg.SvgImage elif method == 'fragment':# Fragment factory (also just a set of rects)factory = qrcode.image.svg.SvgFragmentImage else:# Combined path factory, fixes white space that may occur when zoomingfactory = qrcode.image.svg.SvgPathImageimg = qrcode.make('Some data here', image_factory=factory)

    Two other related factories are available that work the same, but also fill the background of the SVG with white:

    qrcode.image.svg.SvgFillImage qrcode.image.svg.SvgPathFillImage

    Pure Python PNG

    Install the following two packages:

    pip install git+git://github.com/ojii/pymaging.git#egg=pymaging pip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png

    From your command line:

    qr --factory=pymaging "Some text" > test.png

    Or in Python:

    import qrcode from qrcode.image.pure import PymagingImage img = qrcode.make('Some data here', image_factory=PymagingImage)

    參考:https://pypi.python.org/pypi/qrcode/5.1

    總結

    以上是生活随笔為你收集整理的Python二维码生成库qrcode示例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。