{"id":11267,"date":"2024-11-25T21:24:54","date_gmt":"2024-11-26T02:24:54","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=11267"},"modified":"2024-11-25T21:24:55","modified_gmt":"2024-11-26T02:24:55","slug":"qr-code-generation","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=11267","title":{"rendered":"QR Code Generation"},"content":{"rendered":"\n<p>I put together a quick program that creates a &#8220;fancy&#8221; QR code to a specified URL with the specified color and drops the desired &#8220;logo&#8221; file into the center of the code. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport qrcode\nfrom PIL import Image\n\ndef generate_qr_code_with_custom_color_and_logo():\n    url = input(&quot;Please enter the URL for which you want to generate a QR code: &quot;)\n\n    rgb_input = input(&quot;Please enter the RGB values for the QR code color (e.g. 0,0,0 for black): &quot;)\n    \n    try:\n        rgb_color = tuple(map(int, rgb_input.split(&#039;,&#039;)))\n        if len(rgb_color) != 3 or not all(0 &lt;= n &lt;= 255 for n in rgb_color):\n            raise ValueError(&quot;Invalid RGB color value.&quot;)\n    except Exception:\n        print(&quot;Error parsing RGB values. Please make sure to enter three integers separated by commas.&quot;)\n        return\n\n    qr = qrcode.QRCode(\n        version=1,  # controls the size of the QR Code\n        error_correction=qrcode.constants.ERROR_CORRECT_H,  # high error correction for image insertion\n        box_size=10,\n        border=4,\n    )\n    qr.add_data(url)\n    qr.make(fit=True)\n\n    # Generate the QR code with the specified RGB color\n    img = qr.make_image(fill_color=rgb_color, back_color=&quot;white&quot;)\n\n    # Load the logo image\n    logo_image_path = input(&quot;Please enter the logo for the center of this QR code: &quot;)\n\n    try:\n        logo = Image.open(logo_image_path)\n    except FileNotFoundError:\n        print(f&quot;Logo image file &#039;{logo_image_path}&#039; not found. Proceeding without a logo.&quot;)\n        img.save(&quot;qr_code_with_custom_color.png&quot;)\n        print(&quot;QR code has been generated and saved as &#039;qr_code_with_custom_color.png&#039;.&quot;)\n        return\n\n    # Resize the logo image to fit in the QR code\n    img_width, img_height = img.size\n    logo_size = int(img_width * 0.2)  # The logo will take up 20% of the QR code width\n    logo = logo.resize((logo_size, logo_size), Image.ANTIALIAS)\n\n    position = ((img_width - logo_size) \/\/ 2, (img_height - logo_size) \/\/ 2)\n\n    img.paste(logo, position, mask=logo.convert(&quot;RGBA&quot;))\n\n    img.save(&quot;qr_code_with_custom_color_and_logo.png&quot;)\n\n    print(&quot;QR code with a custom color and a logo image has been generated and saved as &#039;qr_code_with_custom_color_and_logo.png&#039;.&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    generate_qr_code_with_custom_color_and_logo()\n<\/pre><\/div>\n\n\n<p>Voila!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2024\/11\/qr_heart.png\"><img loading=\"lazy\" decoding=\"async\" width=\"410\" height=\"410\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2024\/11\/qr_heart.png\" alt=\"\" class=\"wp-image-11268\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2024\/11\/qr_heart.png 410w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2024\/11\/qr_heart-300x300.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2024\/11\/qr_heart-150x150.png 150w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I put together a quick program that creates a &#8220;fancy&#8221; QR code to a specified URL with the specified color and drops the desired &#8220;logo&#8221; file into the center of the code. Voila!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1945],"tags":[664,2065,2066,2067],"class_list":["post-11267","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-qr","tag-qr-code","tag-qr-codes"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11267"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11267\/revisions"}],"predecessor-version":[{"id":11269,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11267\/revisions\/11269"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}