From 8bda8780ce39770684a8c93ae33a49e718567e17 Mon Sep 17 00:00:00 2001 From: Paulo Ricardo Siqueira <37557366+Engcompaulo@users.noreply.github.com> Date: Thu, 31 Oct 2024 18:31:46 -0300 Subject: [PATCH] Refactor QR code generation for flexibility and reuse Function with Parameters: Created a generate_qrcode function that takes parameters for content, colors, and file path, allowing for reuse and flexibility. Comments: Added comments to make the code clearer and easier to understand. Customizable Parameters: Added options for fill and background colors, as well as the file name. Box Size and Border: Set box_size and border to customize the QR code size. --- QR Code Generator/QRcode.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/QR Code Generator/QRcode.py b/QR Code Generator/QRcode.py index adf4bcd0..bd1dffb5 100644 --- a/QR Code Generator/QRcode.py +++ b/QR Code Generator/QRcode.py @@ -1,8 +1,29 @@ import qrcode +from qrcode.constants import ERROR_CORRECT_L -qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L) -qr.add_data("INSERT YOUR LINK HERE") -qr.make(fit=True) +def generate_qrcode(data: str, file_path: str = "qrcode.png", fill_color: str = "black", back_color: str = "white"): + """ + Generates a QR code from the provided data and saves it as an image file. + + Parameters: + - data (str): The content the QR code should contain (URL, text, etc.). + - file_path (str): The path to save the QR code image file (default: "qrcode.png"). + - fill_color (str): Color of the QR code (default: "black"). + - back_color (str): Background color of the QR code (default: "white"). + """ + qr = qrcode.QRCode( + version=1, + error_correction=ERROR_CORRECT_L, + box_size=10, + border=4 + ) + qr.add_data(data) + qr.make(fit=True) + + # Generate the image with specified colors + img = qr.make_image(fill_color=fill_color, back_color=back_color) + img.save(file_path) + print(f"QR code saved as {file_path}") -img = qr.make_image(fill_color="black", back_color="white") -img.save("qrcode.png") +# Usage example +generate_qrcode("https://example.com", "my_qrcode.png")