技术问询:如何修改二维码颜色及Python生成二维码时调整前景与背景色
Hey there! The problem with your current setup is that BitmapImage in Tkinter only handles monochrome (black-and-white) images—so you can't tweak the foreground/background colors directly with that approach.
To get colored QR codes, we'll use the Pillow (PIL) library to generate and customize the image, then display it in your Tkinter window. Here's how to adjust your code step by step:
First, install Pillow if you haven't already:
pip install pillow
Then, here's the modified code with color customization:
from tkinter import * from tkinter import messagebox import os import pyqrcode import io from PIL import Image, ImageTk, ImageOps window = Tk() window.geometry("550x350") window.configure(bg="#0434a0") window.title("QR Code Maker") # Make sure "logo.png" exists in your working directory try: photo = PhotoImage(file="logo.png") window.iconphoto(False, photo) except FileNotFoundError: messagebox.showwarning("Warning", "logo.png not found—skipping icon setup.") def generate(): if len(Subject.get()) == 0: messagebox.showinfo("提示", "请输入URL...") return # Generate QR code and store it in memory (no local file needed) qr = pyqrcode.create(Subject.get()) buffer = io.BytesIO() qr.png(buffer, scale=8) # Save QR as PNG to in-memory buffer buffer.seek(0) # Customize colors using Pillow # Convert to grayscale first for easier colorization grayscale_img = Image.open(buffer).convert("L") # Define your desired colors (use RGB tuples) foreground_color = (102, 0, 204) # Example: Purple foreground background_color = (240, 248, 255) # Example: Alice blue background # Colorize the grayscale QR code colored_img = ImageOps.colorize(grayscale_img, black=foreground_color, white=background_color) # Convert to Tkinter-compatible PhotoImage global photo photo = ImageTk.PhotoImage(colored_img) showcode() def showcode(): imageLabel.config(image=photo) subLabel.config(text=f"QR van {Subject.get()}") # Add your UI elements (make sure these are defined before generate/showcode) Subject = StringVar() entry = Entry(window, textvariable=Subject, width=40) entry.pack(pady=20) generate_btn = Button(window, text="Generate QR Code", command=generate) generate_btn.pack(pady=10) imageLabel = Label(window, bg="#0434a0") imageLabel.pack(pady=10) subLabel = Label(window, text="", bg="#0434a0", fg="white") subLabel.pack(pady=5) window.mainloop()
Key Changes Explained:
- In-Memory Processing: We use
io.BytesIO()to store the QR code PNG in memory instead of saving it to a local file—this keeps things clean and fast. - Pillow Colorization: Converting the QR code to grayscale first lets us use
ImageOps.colorize()to easily swap the default black/white with any RGB colors you want. Just adjust theforeground_colorandbackground_colortuples to match your preferred shades. - Tkinter Compatibility:
ImageTk.PhotoImagereplaces the oldBitmapImageand supports full-color images, so your custom QR code will display correctly in the window.
Feel free to tweak the RGB values to get exactly the colors you need!
内容的提问来源于stack exchange,提问作者TijnVdVelde
相关产品推荐
相关产品推荐

