Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Create Custom QR Codes

Tech Aug 7 24

QR codes are now everywhere, but in this era of personalization, the usual black and white QR codes have become too common. Unique and cute QR codes can catch people's attention.

Have you ever wondered how these interesting QR codes are made? Actually, the key to creating custom QR codes is a third-party library called MyQR.

It is a Python third-party library and currently the most popular QR code generation library in Python. With it, only two lines of code are needed to generate lively and interesting QR codes.

It is like a magic tool for QR codes!

It comes from the sylnsfar/qrcode project on GitHub. This project is very impressive, with over 7100 stars and more than 1200 forks. What's most admirable is that the auther of the source code was still a student at the time, which is amazing!

Using the MyQR library allows you to easily generate QR codes as needed, and you can also set the size of the QR code, embed it in stylized images, or even make it dynamic!

The basic steps are as follows:

Starting from the beginning!

Before generating an eye-catching QR code, we can first experience how the most ordinary QR code is generated:

Install the MyQR Library

Enter pip install MyQR in the system command line and press Enter to run it.

Note that MyQR depends on Python 3, and may not work properly in a Python 2 environment.

# Step1 Import the myqr module
from MyQR import myqr
# Step2 Call the run() method in myqr to generate the QR code
myqr.run(
    words='https://www.boxuegu.com/'
)

After writing the code, run it!

You will see a new image called "qrcode.png" in the current path. That's it!

Code explanation:

We have already installed the MyQR library. In the MyQR library, we often use the module myqr to create QR codes. The import method is from MyQR import myqr.

After importing the module, we can use the run() method in it to generate QR codes.

Let's take a closer look at the parameters inside the myqr.run() method:

In the above code, we only used the "words" parameter, and the rest were default, so a normal black and white QR code without a background image was generated in the current path.

But if it's just this common QR code, isn't it too monotonous?

Can we create something more fun?

Of course we can! And it's especially simple. Just use other parameters in the run() method, for example, adding the desired image to the QR code, which can make the QR code more vibrant!

You'll see the next step! Good luck, ┏ (゜ω゜)=☞

Generate Colorful QR Codes

  1. Prepare a colorful image
  2. Generate a colorful QR code

If we want to embed the above image into our QR code, we need to specify the address of the Boxuegu logo image in the picture parameter of the run() method, and set the name of the new QR code to be generatde in the save_name parameter, such as "Boxuegu logo QR code.png", to avoid conflicts with the previous QR code image.

from MyQR import myqr
myqr.run(
    words='https://www.boxuegu.com/',
    picture='Boxuegu logo.png',
    save_name='Boxuegu logo QR code.png'
)

That's it, run the code! A new QR code named "Boxuegu logo QR code.png" is generated in the current path.

Want to make it more colorful?

Actually, we just missed a parameter. Just set the value of the colorized parameter to True.

from MyQR import myqr
myqr.run(
    words='https://www.boxuegu.com/',
    picture='Boxuegu logo.png',
    save_name='Colorful Boxuegu Logo QR Code.png',
    colorized=True
)

That's it, run the code! Finally, a colorful QR code is generated.

But our creation doesn't end here!

In fact, we can also generate more interesting dynamic QR codes. You can see it in the next step! ┏ (゜ω゜)=☞

Generate Dynamic Personalized QR Codes

Now let's try to create that cool Conan dynamic QR code.

Actually, generating a dynamic QR code is not as complicated as it seems: just replace the static image in the previous codde for generating a static colorful QR code with a dynamic one.

Here, we first prepare a cool and smart Conan GIF!

Prepare the animated GIF

Generate a dynamic personalized QR code

During the process of generating a dynamic QR code, it is worth noting that the file we generate and save must also be in .gif format.

Let's get started quickly!

from MyQR import myqr
myqr.run(
    words='https://www.boxuegu.com',
    picture='Conan.gif',
    save_name='Dynamic Conan QR Code.gif',
    colorized=True
)

Run the code!! duang~~~~ Freshly made dynamic QR code, Dynamic Conan QR Code.gif

If you just want to generate interesting and cute personalized QR codes, this case ends here.

The core of this example is to use the MyQR library effectively:

Following the above three steps, you can easily generate personalized QR codes.

At the same time, you can also try more based on the description of the run() method parameters mentioned earlier, and generate more fun QR codes that meet your needs.

However, if you are diligent and eager to learn, and interested in the principles behind it, you can go to GitHub to read the MyQR source code:

https://github.com/sylnsfar/qrcode

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.