亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python二維碼生成識別實例詳解

 更新時間:2019年07月16日 15:16:40   作者:小柒  
這篇文章主要介紹了Python二維碼生成識別實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

前言

在 JavaWeb 開發(fā)中,一般使用 Zxing 來生成和識別二維碼,但是,Zxing 的識別有點差強人意,不少相對模糊的二維碼識別率很低。不過就最新版本的測試來說,識別率有了現顯著提高。

對比

在沒接觸 Python 之前,曾使用 Zbar 的客戶端進行識別,測了大概幾百張相對模糊的圖片,Zbar的識別速度要快很多,識別率也比 Zxing 稍微準確那邊一丟丟,但是,稍微模糊一點就無法識別。相比之下,微信和支付寶的識別效果就逆天了。

代碼案例

# -*- coding:utf-8 -*-
import os
import qrcode
import time
from PIL import Image
from pyzbar import pyzbar

"""
# 升級 pip 并安裝第三方庫
pip install -U pip
pip install Pillow
pip install pyzbar
pip install qrcode
"""


def make_qr_code_easy(content, save_path=None):
  """
  Generate QR Code by default
  :param content: The content encoded in QR Codeparams
  :param save_path: The path where the generated QR Code image will be saved in.
           If the path is not given the image will be opened by default.
  """
  img = qrcode.make(data=content)
  if save_path:
    img.save(save_path)
  else:
    img.show()


def make_qr_code(content, save_path=None):
  """
  Generate QR Code by given params
  :param content: The content encoded in QR Code
  :param save_path: The path where the generated QR Code image will be saved in.
           If the path is not given the image will be opened by default.
  """
  qr_code_maker = qrcode.QRCode(version=2,
                 error_correction=qrcode.constants.ERROR_CORRECT_M,
                 box_size=8,
                 border=1,
                 )
  qr_code_maker.add_data(data=content)
  qr_code_maker.make(fit=True)
  img = qr_code_maker.make_image(fill_color="black", back_color="white")
  if save_path:
    img.save(save_path)
  else:
    img.show()


def make_qr_code_with_icon(content, icon_path, save_path=None):
  """
  Generate QR Code with an icon in the center
  :param content: The content encoded in QR Code
  :param icon_path: The path of icon image
  :param save_path: The path where the generated QR Code image will be saved in.
           If the path is not given the image will be opened by default.
  :exception FileExistsError: If the given icon_path is not exist.
                This error will be raised.
  :return:
  """
  if not os.path.exists(icon_path):
    raise FileExistsError(icon_path)

  # First, generate an usual QR Code image
  qr_code_maker = qrcode.QRCode(version=4,
                 error_correction=qrcode.constants.ERROR_CORRECT_H,
                 box_size=8,
                 border=1,
                 )
  qr_code_maker.add_data(data=content)
  qr_code_maker.make(fit=True)
  qr_code_img = qr_code_maker.make_image(fill_color="black", back_color="white").convert('RGBA')

  # Second, load icon image and resize it
  icon_img = Image.open(icon_path)
  code_width, code_height = qr_code_img.size
  icon_img = icon_img.resize((code_width // 4, code_height // 4), Image.ANTIALIAS)

  # Last, add the icon to original QR Code
  qr_code_img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8))

  if save_path:
    qr_code_img.save(save_path)
  else:
    qr_code_img.show()


def decode_qr_code(code_img_path):
  """
  Decode the given QR Code image, and return the content
  :param code_img_path: The path of QR Code image.
  :exception FileExistsError: If the given code_img_path is not exist.
                This error will be raised.
  :return: The list of decoded objects
  """
  if not os.path.exists(code_img_path):
    raise FileExistsError(code_img_path)

  # Here, set only recognize QR Code and ignore other type of code
  return pyzbar.decode(Image.open(code_img_path), symbols=[pyzbar.ZBarSymbol.QRCODE], scan_locations=True)


if __name__ == "__main__":

  # # 簡易版
  # make_qr_code_easy("make_qr_code_easy", "make_qr_code_easy.png")
  # results = decode_qr_code("make_qr_code_easy.png")
  # if len(results):
  #   print(results[0].data.decode("utf-8"))
  # else:
  #   print("Can not recognize.")
  #
  # # 參數版
  # make_qr_code("make_qr_code", "make_qr_code.png")
  # results = decode_qr_code("make_qr_code.png")
  # if len(results):
  #   print(results[0].data.decode("utf-8"))
  # else:
  #   print("Can not recognize.")
  #
  # 帶中間 logo 的
  # make_qr_code_with_icon("https://blog.52itstyle.vip", "icon.jpg", "make_qr_code_with_icon.png")
  # results = decode_qr_code("make_qr_code_with_icon.png")
  # if len(results):
  #   print(results[0].data.decode("utf-8"))
  # else:
  #   print("Can not recognize.")

  # 識別答題卡二維碼 16 識別失敗
  t1 = time.time()
  count = 0
  for i in range(1, 33):
    results = decode_qr_code(os.getcwd()+"\\img\\"+str(i)+".png")
    if len(results):
      print(results[0].data.decode("utf-8"))
    else:
      print("Can not recognize.")
      count += 1
  t2 = time.time()
  print("識別失敗數量:" + str(count))
  print("測試時間:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))

測試了32張精挑細選的模糊二維碼:

識別失敗數量:1
測試時間:130

使用最新版的 Zxing 識別失敗了三張。

源碼

https://gitee.com/52itstyle/Python/tree/master/Day13

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • tensorflow使用L2 regularization正則化修正overfitting過擬合方式

    tensorflow使用L2 regularization正則化修正overfitting過擬合方式

    這篇文章主要介紹了tensorflow使用L2 regularization正則化修正overfitting過擬合方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 一篇文章帶你了解python異?;A

    一篇文章帶你了解python異?;A

    今天小編就為大家分享一篇關于Python中的異常介紹,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2021-08-08
  • Pandas條件篩選與組合篩選的使用

    Pandas條件篩選與組合篩選的使用

    本文主要介紹了Pandas條件篩選與組合篩選的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • 一篇文章弄懂Python關鍵字、標識符和變量

    一篇文章弄懂Python關鍵字、標識符和變量

    這篇文章主要給大家介紹了關于Python關鍵字、標識符和變量的相關資料,Python關鍵詞是Python保留的具有特定含義的特殊詞語,用于執(zhí)行某些操作,Python標識符是用戶定義的名稱,而變量是計算機內存中的一塊區(qū)域,存儲對象的內存地址,以便引用對象的值,需要的朋友可以參考下
    2021-07-07
  • pycharm 實現本地寫代碼,服務器運行的操作

    pycharm 實現本地寫代碼,服務器運行的操作

    這篇文章主要介紹了pycharm 實現本地寫代碼,服務器運行的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 基于Linux系統(tǒng)中python matplotlib畫圖的中文顯示問題的解決方法

    基于Linux系統(tǒng)中python matplotlib畫圖的中文顯示問題的解決方法

    下面小編就為大家?guī)硪黄贚inux系統(tǒng)中python matplotlib畫圖的中文顯示問題的解決方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 關于Python列表元素排序操作sort()、min()、max()函數用法

    關于Python列表元素排序操作sort()、min()、max()函數用法

    這篇文章主要介紹了關于Python列表元素排序操作sort()、min()、max()函數用法,python中提供了列表元素的操作函數,那么這些函數該怎么使用呢,感興趣的朋友一起來看看吧
    2023-04-04
  • numpy.transpose()實現數組的轉置例子

    numpy.transpose()實現數組的轉置例子

    今天小編就為大家分享一篇numpy.transpose()實現數組的轉置例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • PyCharm 安裝與使用配置教程(windows,mac通用)

    PyCharm 安裝與使用配置教程(windows,mac通用)

    很多小伙伴下載安裝PyCharm后不會使用,這篇文章詳細介紹了PyCharm安裝與使用教程(windows,mac通用),需要的朋友可以參考下
    2021-05-05
  • pandas中Timestamp類用法詳解

    pandas中Timestamp類用法詳解

    這篇文章主要為大家詳細介紹了pandas中Timestamp類用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12

最新評論