重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Python中怎么生成识别二维码,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序设计、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了江孜免费建站欢迎大家使用!# -*- 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
看完上述内容,你们掌握Python中怎么生成识别二维码的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。