我的git代码仓库: [https://code.aliyun.com/whjwnavy] or [https://github.com/WHJWNAVY]

Python学习:Python Qrcode

设计语言 WHJWNAVY 559℃ 91评论

利用Python qrcode库快速生成二维码

 
 

基本用法:

import qrcode
img = qrcode.make("http://www.wnavy.com/")
img.save('wnavy.png')


 
 

高级用法:

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

 
 

解释一下:

version

一个整数,范围为1到40,表示二维码的大小(最小值是1,是个12×12的矩阵),如果想让程序自动生成,将值设置为 None 并使用 fit=True 参数即可。

error_correction

二维码的纠错范围,可以选择4个常量:

  • ERROR_CORRECT_L      7%以下的错误会被纠正
  • ERROR_CORRECT_M (default)     15%以下的错误会被纠正
  • ERROR_CORRECT_Q   25 %以下的错误会被纠正
  • ERROR_CORRECT_H.     30%以下的错误会被纠正

boxsize

每个点(方块)中的像素个数

border

二维码距图像外围边框距离,默认为4,而且相关规定最小为4

 
 

 
 

生成带有图标的二维码

二维码的容错系数(上面所指的 error_correction)越高,生成的二维码则可允许的残缺率越大,且二维码的数据主要保存在图片的四个角上,所以在二维码中间放一个小图标,对二维码的识别也是不受多大影响的。

对于插入在二维码上的图标大小,这里指定限制图标的大小尺寸最大是二维码长宽的 1/4,以免残缺太大,影响识别。

最后结合 Python 图像库(PIL)的操作,把图片黏贴(paste)在二维码图片的中间,便可以生成一个带有图标的二维码,具体操作代码如下:

from PIL import Image

import qrcode

 
 

qr = qrcode.QRCode(

    version=2,

    error_correction=qrcode.constants.ERROR_CORRECT_H,

    box_size=10,

    border=1

)

 
 

qr.add_data("http://www.wnavy.com/")

qr.make(fit=True)

 
 

img = qr.make_image()

img = img.convert("RGBA")

 
 

icon = Image.open("D:\\xiaohuang.png")

 
 

img_w, img_h = img.size

factor = 4

size_w = int(img_w / factor)

size_h = int(img_h / factor)

 
 

icon_w, icon_h = icon.size

if icon_w > size_w:

    icon_w = size_w

if icon_h > size_h:

    icon_h = size_h

icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)

 
 

w = int((img_w - icon_w) / 2)

h = int((img_h - icon_h) / 2)

img.paste(icon, (w, h), icon)

 
 

img.save("D:\\wnavy_xiaohuang.png")

 
 

 
 


转载请注明:胡椒小兄弟 » Python学习:Python Qrcode

喜欢 (1)or分享 (0)
发表我的评论
取消评论
表情 签到
(91)个小伙伴在吐槽
  1. I simply want to mention I'm all new to weblog and honestly savored this web-site. More than likely I’m likely to bookmark your website . You certainly come with exceptional articles and reviews. Thank you for sharing with us your website page.
    more info here2018-01-31 21:14 回复
  2. I've been absent for some time, but now I remember why I used to love this site. Thank you, I¡¦ll try and check back more often. How frequently you update your website?
    solomon2crm.com2018-02-02 22:50 回复
  3. Hello, you used to write magnificent, but the last several posts have been kinda boring¡K I miss your super writings. Past several posts are just a little bit out of track! come on!
    nationstar mortgage2018-02-03 00:56 回复
  4. I think other web-site proprietors should take this website as an model, very clean and magnificent user genial style and design, let alone the content. You are an expert in this topic!
    5aswiss.com2018-02-03 14:30 回复
  5. Empathetic for your monstrous inspect, in addition I’m just seriously good as an alternative to Zune, and consequently optimism them, together with the very good critical reviews some other players have documented, will let you determine whether it does not take right choice for you.
    匿名2018-02-03 16:12 回复
  6. My brother recommended I might like this website. He was totally right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!
  7. I cling on to listening to the newscast lecture about getting free online grant applications so I have been looking around for the most excellent site to get one. Could you tell me please, where could i get some?
    brandsfavor.com2018-02-06 03:50 回复
  8. I have been examinating out a few of your stories and i can state nice stuff. I will definitely bookmark your website.
  9. Hello. impressive job. I did not expect this. This is a great story. Thanks!
    sci tech2018-02-06 05:58 回复
  10. hey there and thank you for your information – I’ve certainly picked up something new from right here. I did however expertise several technical points using this site, since I experienced to reload the web site a lot of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I am adding this RSS to my email and could look out for much more of your respective exciting content. Ensure that you update this again very soon..
    chanelpascher.com2018-02-06 10:45 回复
  11. Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is excellent, let alone the content!
    consultarnumero.com2018-02-06 11:18 回复
  12. certainly like your web site however you need to check the spelling on several of your posts. Many of them are rife with spelling issues and I find it very bothersome to tell the reality however I will surely come again again.
    healthexpo2015.net2018-02-06 18:58 回复
  13. I have not checked in here for a while as I thought it was getting boring, but the last several posts are great quality so I guess I¡¦ll add you back to my daily bloglist. You deserve it my friend :)
    crammonduk.com2018-02-07 05:22 回复
  14. I was just searching for this info for a while. After 6 hours of continuous Googleing, finally I got it in your web site. I wonder what is the lack of Google strategy that do not rank this type of informative sites in top of the list. Usually the top sites are full of garbage.
    nationstar mortgage2018-02-07 09:40 回复
  15. I wish to express appreciation to you for bailing me out of this type of crisis. Right after checking throughout the world-wide-web and finding basics that were not beneficial, I thought my life was well over. Being alive devoid of the strategies to the difficulties you have solved by way of your good short post is a critical case, as well as the kind which may have in a wrong way damaged my entire career if I hadn't noticed the blog. Your main capability and kindness in controlling all the pieces was valuable. I'm not sure what I would have done if I hadn't discovered such a subject like this. I am able to at this moment look forward to my future. Thanks a lot so much for the expert and result oriented help. I won't be reluctant to refer your web sites to anybody who should have counselling about this subject matter.
  16. What i don't understood is in fact how you are no longer really much more well-favored than you may be right now. You're very intelligent. You already know therefore significantly with regards to this subject, produced me personally consider it from so many various angles. Its like women and men are not interested unless it is something to accomplish with Girl gaga! Your individual stuffs excellent. Always care for it up!
    Fashion2018-02-07 14:25 回复
  17. Good blog! I really love how it is easy on my eyes and the data are well written. I'm wondering how I might be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a great day!
    ircyb.org2018-02-07 20:18 回复
  18. you're in reality a good webmaster. The site loading pace is amazing. It kind of feels that you are doing any unique trick. Moreover, The contents are masterpiece. you've performed a magnificent activity on this subject!
    Fashion2018-02-08 00:16 回复
  19. You actually make it appear so easy along with your presentation however I find this topic to be actually one thing that I believe I'd by no means understand. It kind of feels too complex and very huge for me. I am looking forward for your subsequent put up, I¡¦ll try to get the grasp of it!
    kenoshanow.com2018-02-08 10:00 回复
  20. I've been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.
    Home Improvement2018-02-08 15:05 回复
  21. I'm just commenting to make you be aware of what a nice encounter our daughter developed reading your web page. She figured out so many details, not to mention what it's like to possess an amazing giving spirit to have most people without problems gain knowledge of specified complex subject matter. You really did more than my desires. Many thanks for distributing those warm and helpful, dependable, educational and in addition fun tips on the topic to Jane.
  22. Thanks for every one of your effort on this website. My mom really loves doing investigations and it's really easy to see why. Most of us know all concerning the dynamic tactic you render helpful guidance by means of this web blog and as well encourage participation from other people on this idea plus our own girl is studying a whole lot. Take pleasure in the rest of the new year. You are always carrying out a first class job.
    dinner recipes2018-02-08 23:47 回复
  23. We are a group of volunteers and opening a new scheme in our community. Your web site provided us with valuable info to work on. You've done an impressive job and our entire community will be thankful to you.
    football2018-02-08 23:48 回复
  24. Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently quickly.
    Smartphone2018-02-08 23:52 回复
  25. I have to voice my love for your kind-heartedness for men and women who require help on this one idea. Your special commitment to passing the message throughout has been extraordinarily effective and have frequently allowed guys just like me to arrive at their ambitions. Your valuable guideline can mean this much to me and especially to my fellow workers. Many thanks; from each one of us.
    effortlessrun.com2018-02-09 00:20 回复
  26. It¡¦s really a cool and useful piece of info. I¡¦m satisfied that you shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.
    fun games2018-02-09 00:36 回复
  27. I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You've made my day! Thanks again!
    Home Improvement2018-02-09 03:27 回复
  28. You have brought up a very superb points , thanks for the post.
    News2018-02-09 13:50 回复
  29. Thank you for helping out, wonderful info. "The surest way to be deceived is to think oneself cleverer than the others." by La Rochefoucauld.
    News2018-02-09 22:45 回复
  30. I definitely wanted to write down a word to thank you for all of the fantastic points you are writing on this website. My extended internet research has now been recognized with useful ideas to exchange with my good friends. I 'd believe that most of us readers are truly blessed to be in a decent community with very many special professionals with useful pointers. I feel very privileged to have encountered your entire website and look forward to many more thrilling moments reading here. Thanks a lot once more for a lot of things.
    News2018-02-10 13:56 回复
  31. I really enjoy examining on this website , it has good content . "When a man's willing and eager, the gods join in." by Aeschylus.
    News2018-02-11 03:01 回复
  32. As soon as I observed this internet site I went on reddit to share some of the love with them.
    News2018-02-11 15:54 回复
  33. I loved as much as you will receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike.
    Fashion2018-02-12 05:35 回复
  34. I¡¦m not positive the place you are getting your information, but great topic. I must spend some time finding out more or working out more. Thank you for magnificent info I used to be on the lookout for this info for my mission.
  35. Pretty nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again soon!
    faturaodemeleri.com2018-02-12 12:21 回复
  36. Good – I should certainly pronounce, impressed with your website. I had no trouble navigating through all tabs and related info ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or something, web site theme . a tones way for your client to communicate. Nice task.
    News2018-02-12 16:33 回复
  37. Thank you for sharing superb informations. Your web site is very cool. I'm impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the information I already searched everywhere and simply couldn't come across. What a great website.
    News2018-02-13 01:32 回复
  38. I needed to create you this very small remark to be able to thank you so much the moment again for your splendid opinions you have featured at this time. It is certainly extremely generous with people like you to deliver openly what exactly some people would've distributed for an e-book to generate some bucks for their own end, especially now that you might well have done it in the event you desired. The things additionally served as a fantastic way to be certain that other people online have the identical dream just as mine to know the truth a little more with regard to this matter. I'm certain there are thousands of more fun occasions up front for people who read through your blog.
  39. You are my breathing in, I own few blogs and often run out from to post .
    News2018-02-13 21:13 回复
  40. Thanks for sharing excellent informations. Your web-site is so cool. I am impressed by the details that you have on this website. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found just the info I already searched everywhere and just couldn't come across. What an ideal web-site.
    News2018-02-14 05:32 回复
  41. Great blog here! Also your website loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol
    poia.info2018-02-14 09:33 回复
  42. Some genuinely nice and useful info on this web site, as well I believe the design and style contains great features.
    Fashion2018-02-14 13:50 回复
  43. My brother recommended I might like this website. He was entirely right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!
    Home Improvement2018-02-15 03:54 回复
  44. I’m not sure where you're getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for wonderful info I was looking for this info for my mission.
    Home Improvement2018-02-15 12:54 回复
  45. I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this increase.
    poia.info2018-02-15 14:35 回复
  46. I got what you mean , thanks for putting up.Woh I am pleased to find this website through google. "Don't be afraid of opposition. Remember, a kite rises against not with the wind." by Hamilton Mabie.
    Home Improvement2018-02-16 01:22 回复
  47. Very interesting subject, thank you for posting.
    News2018-02-16 16:13 回复
  48. Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch as I found it for him smile Thus let me rephrase that: Thanks for lunch!
    Home2018-02-16 23:42 回复
  49. Hi there, just became alert to your blog through Google, and found that it is truly informative. I’m going to watch out for brussels. I will be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers!
  50. I really appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You've made my day! Thank you again!
    Home Improvement2018-02-17 01:11 回复
  51. I think you have noted some very interesting details , thanks for the post.
    Home Improvement2018-02-17 17:20 回复
  52. We are a group of volunteers and opening a new scheme in our community. Your website offered us with valuable info to work on. You have done an impressive job and our entire community will be grateful to you.
    cathode2018-02-18 00:20 回复
  53. I have recently started a blog, the information you offer on this website has helped me tremendously. Thanks for all of your time & work.
    home values2018-02-18 00:20 回复
  54. Hiya, I'm really glad I have found this information. Nowadays bloggers publish just about gossips and web and this is really annoying. A good web site with interesting content, that's what I need. Thanks for keeping this website, I'll be visiting it. Do you do newsletters? Can not find it.
    cheap plane tickets2018-02-18 00:21 回复
  55. Useful information. Lucky me I discovered your website accidentally, and I am stunned why this twist of fate didn't came about earlier! I bookmarked it.
    student work2018-02-18 00:26 回复
  56. Thanks a bunch for sharing this with all folks you actually recognise what you are speaking approximately! Bookmarked. Kindly additionally discuss with my website =). We could have a hyperlink exchange arrangement among us!
    businesses2018-02-18 00:33 回复
  57. I have recently started a web site, the information you provide on this web site has helped me greatly. Thanks for all of your time & work. "If you would know strength and patience, welcome the company of trees." by Hal Borland.
    Health2018-02-18 01:51 回复
  58. Nice read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch as I found it for him smile Therefore let me rephrase that: Thanks for lunch! "Love is made in heaven and consummated on earth." by John Lyly.
    Home Improvement2018-02-18 14:52 (7天前)回复
  59. I loved as much as you'll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this increase.
    Home Improvement2018-02-18 22:27 (7天前)回复
  60. I was looking at some of your articles on this internet site and I believe this web site is very instructive! Keep on putting up.
    Fashion2018-02-19 12:14 (6天前)回复
  61. Wonderful goods from you, man. I have understand your stuff previous to and you are just too excellent. I really like what you've acquired here, certainly like what you're stating and the way in which you say it. You make it entertaining and you still care for to keep it smart. I cant wait to read far more from you. This is actually a wonderful website.
    Web Design2018-02-19 20:21 (6天前)回复
  62. Thank you for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such magnificent info being shared freely out there.
    Fashion2018-02-20 04:02 (5天前)回复
  63. Terrific work! That is the kind of information that should be shared across the internet. Disgrace on the search engines for now not positioning this post higher! Come on over and visit my website . Thank you =)
    sports and arts2018-02-20 12:28 (5天前)回复
  64. A lot of thanks for your entire labor on this website. My mom take interest in working on investigations and it's really simple to grasp why. A lot of people notice all about the lively mode you create priceless solutions by means of the web site and recommend response from some other people on this point and my girl has been understanding so much. Have fun with the rest of the new year. You are carrying out a useful job.
    international flights2018-02-20 12:29 (5天前)回复
  65. I simply desired to appreciate you yet again. I'm not certain the things that I would have tried without the actual strategies documented by you over such area of interest. This was a alarming issue in my opinion, but taking a look at the very specialized manner you resolved the issue forced me to leap over contentment. I'm happy for the guidance as well as expect you know what a great job you are doing training men and women all through a site. I'm certain you've never encountered any of us.
    laptop2018-02-20 12:33 (5天前)回复
  66. I like the helpful info you provide in your articles. I will bookmark your weblog and check again here regularly. I am quite certain I will learn a lot of new stuff right here! Good luck for the next!
    auto sales2018-02-20 12:36 (5天前)回复
  67. I've recently started a website, the info you offer on this web site has helped me tremendously. Thanks for all of your time & work. "'Tis our true policy to steer clear of permanent alliances with any portion of the foreign world." by George Washington.
    Home Improvement2018-02-20 14:39 (5天前)回复
  68. You are my intake , I own few blogs and rarely run out from to brand.I think this internet site has some very great information for everyone. "He who has not looked on Sorrow will never see Joy." by Kahlil Gibran.
    Home Improvement2018-02-20 23:07 (5天前)回复
  69. It's the best time to make some plans for the future and it's time to be happy. I've read this post and if I could I want to suggest you few interesting things or suggestions. Maybe you could write next articles referring to this article. I desire to read more things about it!
    Sports Medicine2018-02-21 03:36 (4天前)回复
  70. Hey There. I found your blog using msn. This is an extremely well written article. I’ll be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.
    best online shopping2018-02-21 03:37 (4天前)回复
  71. I do not even know how I ended up here, but I thought this post was good. I do not know who you are but certainly you are going to a famous blogger if you aren't already 😉 Cheers!
    kitchen ideas2018-02-21 03:38 (4天前)回复
  72. Nice weblog here! Also your web site a lot up very fast! What host are you the use of? Can I am getting your associate link in your host? I want my site loaded up as quickly as yours lol
    home remodeling2018-02-21 03:38 (4天前)回复
  73. I delight in, cause I discovered just what I was looking for. You have ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye
    kitchen ideas2018-02-21 03:54 (4天前)回复
  74. Some really fantastic posts on this web site, regards for contribution. "Once, power was considered a masculine attribute. In fact, power has no sex." by Katharine Graham.
    Travel2018-02-21 07:19 (4天前)回复
  75. I have been examinating out a few of your stories and i can claim pretty clever stuff. I will definitely bookmark your site.
    casual dating2018-02-21 17:32 (4天前)回复
  76. hi!,I love your writing so much! percentage we communicate extra about your post on AOL? I need a specialist in this area to solve my problem. Maybe that's you! Taking a look forward to peer you.
    hotel2018-02-21 17:33 (4天前)回复
  77. Very interesting subject, thank you for putting up.
    Jewelry2018-02-21 17:55 (4天前)回复
  78. What i don't realize is if truth be told how you're not actually much more well-appreciated than you might be right now. You are very intelligent. You realize therefore considerably on the subject of this topic, made me individually imagine it from so many numerous angles. Its like men and women aren't interested unless it is one thing to accomplish with Woman gaga! Your personal stuffs excellent. All the time maintain it up!
    Fashion2018-02-22 13:21 (3天前)回复
  79. Very interesting subject , appreciate it for putting up. "The great leaders have always stage-managed their effects." by Charles De Gaulle.
    Home Improvement2018-02-22 21:10 (3天前)回复
  80. I've learn several excellent stuff here. Definitely value bookmarking for revisiting. I wonder how a lot attempt you place to make any such great informative website.
    Health2018-02-23 05:16 (2天前)回复
  81. Simply a smiling visitor here to share the love (:, btw great style and design. "Individuals may form communities, but it is institutions alone that can create a nation." by Benjamin Disraeli.
    Health2018-02-23 13:18 (2天前)回复
  82. Heya i’m for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you aided me.
    Web Design2018-02-24 01:49 (1天前)回复
  83. Thank you for every other fantastic article. The place else could anyone get that type of info in such a perfect manner of writing? I've a presentation subsequent week, and I'm at the look for such info.
    technology current events2018-02-24 02:59 (1天前)回复
  84. It's perfect time to make some plans for the future and it's time to be happy. I have read this post and if I could I wish to suggest you some interesting things or tips. Perhaps you could write next articles referring to this article. I desire to read more things about it!
    international flights2018-02-24 03:20 (1天前)回复
  85. Hey there, You have done an excellent job. I’ll definitely digg it and personally suggest to my friends. I'm confident they will be benefited from this web site.
    Global News2018-02-24 10:36 (1天前)回复
  86. Whats up very cool blog!! Man .. Excellent .. Superb .. I will bookmark your website and take the feeds additionally¡KI'm satisfied to search out numerous helpful info here in the put up, we'd like develop extra techniques on this regard, thanks for sharing. . . . . .
    Law2018-02-24 10:36 (1天前)回复
  87. I simply wished to thank you very much all over again. I'm not certain what I might have gone through in the absence of the actual tips discussed by you over that field. It previously was a real terrifying setting in my circumstances, nevertheless coming across the expert strategy you handled it forced me to leap for happiness. I'm just grateful for this assistance and even trust you comprehend what a great job you were accomplishing educating the rest via your web blog. I am certain you've never met any of us.
    Pets2018-02-24 10:46 (1天前)回复
  88. I needed to compose you this little observation to be able to thank you very much once again for these lovely methods you've discussed above. This has been quite pretty generous with people like you to give without restraint exactly what a number of people could have offered as an e book to generate some money for their own end, mostly given that you might well have tried it if you decided. These smart ideas also acted to become fantastic way to comprehend other people online have the identical zeal like my personal own to find out good deal more when it comes to this issue. I am certain there are millions of more pleasurable opportunities in the future for individuals that see your site.
    Entertainment2018-02-24 10:47 (1天前)回复
  89. You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!
    Home Improvement2018-02-24 11:40 (1天前)回复
  90. You are my aspiration , I have few blogs and sometimes run out from to post .
    Travel2018-02-24 19:33 (18小时前)回复
  91. Keep up the excellent work , I read few content on this site and I believe that your weblog is really interesting and contains sets of excellent info .
    Business2018-02-25 05:17 (8小时前)回复