Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions QR_code_generator.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import qrcode

# Enter url of any website here.
input_URL = "https://www.google.com/"

qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=15,
border=4,
)

qr.add_data(input_URL)
qr.make(fit=True)

# convert into image
img = qr.make_image(fill_color="red", back_color="white")
img.save("url_qrcode.png")

print(qr.data_list)
import qrcode


def generate_qr_code(data, fill_color='black', back_color='white', file_name='qrcode.png'):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)

img = qr.make_image(fill_color=fill_color, back_color=back_color)
img.save(file_name)
return img


def main():
# Enter url of any website here.
input_URL = 'https://www.google.com/'
# Generate QR code
generate_qr_code(input_URL, fill_color='red', back_color='white', file_name='url_qrcode.png')


if __name__ == '__main__':
main()
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# QR code generator using Python
This script take a link of any URL and generate a `QR code` corresponding to it.
This script takes a link of any URL and generates a `QR code` corresponding to it.
### Prerequisites
`Python 3`
### Library Used
Expand All @@ -12,6 +12,10 @@ Run `pip install qrcode`
- Provide your desired URL in the script
- Execute `python3 QR_code_generator.py`

### How to run the unit tests
- Ensure that the testing library (e.g., `unittest` or `pytest`) is installed
- Run `python -m unittest discover` or `pytest` depending on the testing tool used

### Screenshot showing the sample use of the script
<p align="center">
<a href="output 1.png"><img src="https://user-images.githubusercontent.com/85709371/151921721-132e76c1-1604-49ad-9234-1ef3cc9ac45b.png" alt="url_qrcode"></a>
Expand Down
34 changes: 34 additions & 0 deletions tests/test_QR_code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
import os
from QR_code_generator import generate_qr_code


class TestQRCodeGenerator(unittest.TestCase):

def test_valid_input(self):
# Test case for valid input
input_data = 'Hello, World!'
output_file = 'test_output.png'
generate_qr_code(input_data, output_file)
self.assertTrue(os.path.exists(output_file))
os.remove(output_file)

def test_invalid_input(self):
# Test case for invalid input
input_data = ''
output_file = 'test_output.png'
with self.assertRaises(ValueError):
generate_qr_code(input_data, output_file)

def test_file_output(self):
# Test case for file output
input_data = 'Hello, World!'
output_file = 'test_output.png'
generate_qr_code(input_data, output_file)
self.assertTrue(os.path.exists(output_file))
# Add more assertions if necessary
os.remove(output_file)


if __name__ == '__main__':
unittest.main()