Python QR Code Generator with Custom Colors and Logo

This article demonstrates how to create a customizable QR code generator in Python using the qrcode and Pillow libraries, featuring a Tkinter GUI for inputting text, selecting fill and background colors, and embedding a logo image into the generated QR code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python QR Code Generator with Custom Colors and Logo

QR codes, also known as two‑dimensional barcodes, are widely used; this tutorial explains how to quickly generate a QR code with Python, allowing customization of background, fill color, and an optional central logo.

First, install the required third‑party libraries:

pip install qrcode
pip install pillow  # Pillow replaces the older PIL library for Python 3+

The qrcode.QRCode class accepts several parameters: version (size of the matrix, 1‑40), error_correction (L, M, Q, H levels), box_size (pixel size of each box), and border (width of the white border).

A Tkinter window is created to let the user enter the text or URL, choose fill and background colors, and press a button to generate the QR code. The GUI code is shown below:

if __name__ == '__main__':
    window = tk.Tk()
    window.title('二维码生成器')
    window.geometry('900x600')
    label_url = tk.Label(window, text="输入文本:")
    label_url.place(x=50, y=40)
    Entry_input_url = tk.Entry()
    Entry_input_url.place(x=120, y=40)
    label_fill_color = tk.Label(window, text="填充颜色:")
    label_fill_color.place(x=50, y=80)
    Entry_input_fill_color = tk.Entry()
    Entry_input_fill_color.place(x=120, y=80)
    label_back_color = tk.Label(window, text="背景颜色:")
    label_back_color.place(x=50, y=120)
    Entry_input_back_color = tk.Entry()
    Entry_input_back_color.place(x=120, y=120)
    button = tk.Button(window, text='开始生成', command=create_code)
    button.place(x=140, y=160)
    window.mainloop()

The create_code function builds the QR code, converts it to RGBA, loads a logo image, resizes the logo to fit within the QR code, and pastes it at the center. The final image is saved as qr.png and displayed in the Tkinter window.

def create_code():
    content = Entry_input_url.get()
    fill_color = Entry_input_fill_color.get()
    back_color = Entry_input_back_color.get()
    qr = qrcode.QRCode(version=2,
                       error_correction=qrcode.constants.ERROR_CORRECT_H,
                       box_size=6,
                       border=2)
    qr.add_data(content)
    qr.make(fit=True)
    img = qr.make_image(fill_color=fill_color, back_color=back_color)
    img = img.convert("RGBA")
    icon = Image.open("2.jpg")
    img_w, img_h = img.size
    factor = 4
    size_w, size_h = int(img_w / factor), 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, h = int((img_w - icon_w) / 2), int((img_h - icon_h) / 2)
    icon = icon.convert("RGBA")
    img.paste(icon, (w, h), icon)
    img.save("qr.png")
    photo = tk.PhotoImage(file="qr.png")
    Label_img = tk.Label(window, image=photo)
    Label_img.place(x=300, y=300)

The article concludes with a disclaimer that the content is collected from the internet and the original author retains copyright.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonQR code
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.