Skip to content

Commit 4f12088

Browse files
authored
Refactor QR code generation for flexibility and reuse (DhanushNehru#373)
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.
1 parent f9cfa25 commit 4f12088

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

QR Code Generator/QRcode.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
import qrcode
2+
from qrcode.constants import ERROR_CORRECT_L
23

3-
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L)
4-
qr.add_data("INSERT YOUR LINK HERE")
5-
qr.make(fit=True)
4+
def generate_qrcode(data: str, file_path: str = "qrcode.png", fill_color: str = "black", back_color: str = "white"):
5+
"""
6+
Generates a QR code from the provided data and saves it as an image file.
7+
8+
Parameters:
9+
- data (str): The content the QR code should contain (URL, text, etc.).
10+
- file_path (str): The path to save the QR code image file (default: "qrcode.png").
11+
- fill_color (str): Color of the QR code (default: "black").
12+
- back_color (str): Background color of the QR code (default: "white").
13+
"""
14+
qr = qrcode.QRCode(
15+
version=1,
16+
error_correction=ERROR_CORRECT_L,
17+
box_size=10,
18+
border=4
19+
)
20+
qr.add_data(data)
21+
qr.make(fit=True)
22+
23+
# Generate the image with specified colors
24+
img = qr.make_image(fill_color=fill_color, back_color=back_color)
25+
img.save(file_path)
26+
print(f"QR code saved as {file_path}")
627

7-
img = qr.make_image(fill_color="black", back_color="white")
8-
img.save("qrcode.png")
28+
# Usage example
29+
generate_qrcode("https://example.com", "my_qrcode.png")

0 commit comments

Comments
 (0)