{"id":329510,"date":"2023-09-06T14:00:00","date_gmt":"2023-09-06T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python-generate-qr-code\/"},"modified":"2023-09-06T14:00:00","modified_gmt":"2023-09-06T14:00:00","slug":"generate-beautiful-qr-codes-with-python","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/06\/generate-beautiful-qr-codes-with-python\/","title":{"rendered":"Generate Beautiful QR Codes With Python"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p>From restaurant e-menus to airline boarding passes, <strong>QR codes<\/strong> have numerous applications that impact your day-to-day life and enrich the user\u2019s experience. Wouldn\u2019t it be great to make them look good, too? With the help of this tutorial, you\u2019ll learn how to use Python to generate beautiful QR codes for your personal use case. <\/p>\n<p>In its most basic format, a QR code contains black squares and dots on a white background, with information that any smartphone or device with a dedicated QR scanner can decode. Unlike a traditional bar code, which holds informationation horizontally, a QR code holds the data in two dimensions, and it can hold over a hundred times more information.<\/p>\n<p><strong>In this tutorial, you\u2019ll learn how to:<\/strong><\/p>\n<ul>\n<li><strong>Generate<\/strong> a basic black-and-white QR code<\/li>\n<li>Change the <strong>size<\/strong> and <strong>margins<\/strong> of the QR code<\/li>\n<li>Create <strong>colorful<\/strong> QR codes<\/li>\n<li><strong>Rotate<\/strong> the QR code<\/li>\n<li>Replace the static background with an <strong>animated GIF<\/strong><\/li>\n<\/ul>\n<p>Traditionally, QR codes have been predominantly black-and-white. Now, thanks to creative innovations, QR codes come in all sorts of shapes, sizes, and colors. If you\u2019d like to learn how to create not only two-tone QR codes but also colorful and artistic ones, then this is the tutorial for you.<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Free Bonus:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/python-generate-qr-code-bonus\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download the sample code<\/a> that shows you how to generate a beautiful code with Python.<\/p>\n<\/div>\n<h2 id=\"using-python-to-generate-a-basic-qr-code\">Using Python to Generate a Basic QR Code<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python-generate-qr-code\/#using-python-to-generate-a-basic-qr-code\" title=\"Permanent link\"><\/a><\/h2>\n<p>To begin with, you\u2019re going to create a black-and-white QR code that encodes some content. To follow along with this tutorial, you\u2019ll need to install <a href=\"https:\/\/segno.readthedocs.io\/en\/latest\/\">Segno<\/a>, which is a popular Python library for generating QR codes. To install Segno, you can run <a href=\"https:\/\/realpython.com\/what-is-pip\/\"><code>pip<\/code><\/a> from your <a href=\"https:\/\/realpython.com\/python-command-line-arguments\/#the-command-line-interface\">command line<\/a>: <\/p>\n<div class=\"highlight sh\">\n<pre><span><\/span><code><span class=\"gp\">$ <\/span>python<span class=\"w\"> <\/span>-m<span class=\"w\"> <\/span>pip<span class=\"w\"> <\/span>install<span class=\"w\"> <\/span>segno\n<\/code><\/pre>\n<\/div>\n<p>Once you\u2019ve installed <code>segno<\/code>, create a Python file named <code>basic_qrcode.py<\/code>. To create a black-and-white QR code object that encodes some content, you\u2019ll have to use the <code>make_qr()<\/code> function. This ensures that you\u2019re creating a full-size QR code, and the only mandatory argument you\u2019ll need to pass is the information that you want to encode. <\/p>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> You can also use the <a href=\"https:\/\/segno.readthedocs.io\/en\/stable\/api.html#segno.make\"><code>make()<\/code><\/a> function to create a QR code object, but depending on the content you\u2019re encoding, it might create a micro QR code. For the sake of this tutorial, you can think of that as a smaller QR code. To enforce the creation of a full-size QR code, you should use the <code>make_qr()<\/code> function.<\/p>\n<\/div>\n<p>QR codes are capable of handling all types of data, such as alphanumeric characters, symbols, and even URLs. To begin your journey of generating QR codes in Python, you\u2019ll start by creating a QR code object that will encode the text <code>\"Hello, World\"<\/code>:<\/p>\n<div class=\"highlight python\">\n<pre><span><\/span><code><span class=\"c1\"># basic_qrcode.py<\/span>\n\n<span class=\"kn\">import<\/span> <span class=\"nn\">segno<\/span>\n\n<span class=\"n\">segno<\/span><span class=\"o\">.<\/span><span class=\"n\">make_qr<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"Hello, World\"<\/span><span class=\"p\">)<\/span>\n<\/code><\/pre>\n<\/div>\n<p>At this stage, you\u2019ve written the code to encode the text <code>\"Hello, World\"<\/code> by passing this to the <code>make_qr()<\/code> function. This will greet your user with that text, and it might also offer the option of searching for <code>\"Hello, World\"<\/code> on the Internet.<\/p>\n<p>Now, to generate a black-and-white QR code that you can actually view and scan, you\u2019ll need to store the encoded content as a variable and use the <a href=\"https:\/\/segno.readthedocs.io\/en\/stable\/api.html#segno.QRCode.save\"><code>.save()<\/code><\/a> method to save your QR code as an image. Here\u2019s an example of how you can create a <a href=\"https:\/\/realpython.com\/python-variables\/\">variable<\/a> called <code>qrcode<\/code> that encodes the text <code>\"Hello, World\"<\/code> as a black-and-white QR code object, which you then save as a PNG file called <code>basic_qrcode.png<\/code>: <\/p>\n<div class=\"highlight python\">\n<pre><span><\/span><code><span class=\"c1\"># basic_qrcode.py<\/span>\n\n<span class=\"kn\">import<\/span> <span class=\"nn\">segno<\/span>\n\n<span class=\"n\">qrcode<\/span> <span class=\"o\">=<\/span> <span class=\"n\">segno<\/span><span class=\"o\">.<\/span><span class=\"n\">make_qr<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"Hello, World\"<\/span><span class=\"p\">)<\/span>\n<span class=\"hll\"><span class=\"n\">qrcode<\/span><span class=\"o\">.<\/span><span class=\"n\">save<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"basic_qrcode.png\"<\/span><span class=\"p\">)<\/span>\n<\/span><\/code><\/pre>\n<\/div>\n<p>The <code>.save()<\/code> method serializes the QR code into a file format of your choice, as long as the chosen format is supported. When you apply <code>.save()<\/code> to the variable that you\u2019ve created with the encoded content, you need to specify the filename including an optional file path. In the example above, you\u2019re saving the QR code image as a file named <code>basic_qrcode.png<\/code> in the same directory where you\u2019ll be executing your code, so you don\u2019t specify a file path. <\/p>\n<p>If you\u2019d like to save the image in a different directory, then you can specify the desired <a href=\"https:\/\/realpython.com\/python-pathlib\/\">file path<\/a> together with the filename as an argument in the <code>.save()<\/code> method.<\/p>\n<p>Once you\u2019ve finished writing the steps for generating a black-and-white QR code in <code>basic_qrcode.py<\/code>, you\u2019ll need to <a href=\"https:\/\/realpython.com\/run-python-scripts\/\">run the Python script<\/a> from your command line:<\/p>\n<div class=\"highlight sh\">\n<pre><span><\/span><code><span class=\"gp\">$ <\/span>python<span class=\"w\"> <\/span>basic_qrcode.py\n<\/code><\/pre>\n<\/div>\n<p>Congratulations, you\u2019ve just created a black-and-white QR code that encodes the text <code>\"Hello, World\"<\/code> using <code>make_qr()<\/code> and <code>.save()<\/code>. You can now scan your QR code image, which you\u2019ll find in the same directory that you\u2019re running your code from. Or, you can scan the QR code image below:<\/p>\n<figure class=\"js-lightbox\"><a href=\"https:\/\/files.realpython.com\/media\/my_first_qrcode.b078ef62bf2b.png\" ><img decoding=\"async\" loading=\"lazy\" class=\"img-fluid mx-auto d-block \" src=\"https:\/\/files.realpython.com\/media\/my_first_qrcode.b078ef62bf2b.png\" width=\"29\" height=\"29\" srcset=\"https:\/\/robocrop.realpython.net\/?url=https%3A\/\/files.realpython.com\/media\/my_first_qrcode.b078ef62bf2b.png&amp;w=7&amp;sig=103566dafee2d7f37ef8a26307e21a5fd7cdd840 7w, https:\/\/robocrop.realpython.net\/?url=https%3A\/\/files.realpython.com\/media\/my_first_qrcode.b078ef62bf2b.png&amp;w=9&amp;sig=79cd20a548f17a00a25bcc52abcbeca16bada377 9w, https:\/\/robocrop.realpython.net\/?url=https%3A\/\/files.realpython.com\/media\/my_first_qrcode.b078ef62bf2b.png&amp;w=14&amp;sig=508bd9e1858b7b4f28d4235399c18286a4433bfe 14w, https:\/\/files.realpython.com\/media\/my_first_qrcode.b078ef62bf2b.png 29w\" sizes=\"(min-width: 1200px) 690px, (min-width: 780px) calc(-5vw + 669px), (min-width: 580px) 510px, calc(100vw - 30px)\" alt=\"An image of a basic black and white QR Code\" data-asset=\"5206\"><\/a><\/figure>\n<p>You may have found that the QR code image is a little difficult to view or read because of its size, so the next item that you\u2019re going to adjust is the size of your basic QR code.<\/p>\n<h2 id=\"changing-the-size-the-qr-code\">Changing the Size the QR Code<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python-generate-qr-code\/#changing-the-size-the-qr-code\" title=\"Permanent link\"><\/a><\/h2>\n<p>When trying to scan your QR code image or the QR code in the tutorial, you might have found it difficult to read because of its size.<\/p>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python-generate-qr-code\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python-generate-qr-code\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this tutorial, you&#8217;ll learn how to use Python to generate QR codes, from your standard black-and-white QR codes to beautiful ones with your favorite colors. You&#8217;ll learn how to format QR codes, rotate them, and even replace the static background with moving images.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Generate Beautiful QR Codes With Python - ITTeacherITFreelance.hk","description":"In this tutorial, you'll learn how to use Python to generate QR codes, from your standard black-and-white QR codes to beautiful ones with your favorite colors."},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329510"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329510"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329510\/revisions"}],"predecessor-version":[{"id":329511,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329510\/revisions\/329511"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}