Share python qrcode

namviet265

New member
## Cách tạo mã QR trong Python

Mã QR là một cách nhanh chóng và dễ dàng để chia sẻ thông tin.Chúng có thể được sử dụng để lưu trữ URL, thông tin liên hệ, phiếu giảm giá, v.v.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo mã QR trong Python.

### Điều kiện tiên quyết

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một thông dịch viên Python
* [Gối] (Pillow) thư viện

### Tạo mã QR

Để tạo mã QR, chúng tôi sẽ sử dụng mô -đun `qrcode` từ thư viện gối.Mô -đun `QRCode` cung cấp một số chức năng để tạo và giải mã mã QR.

Để tạo mã QR, chúng ta có thể sử dụng hàm `qrcode ()`.Hàm này có một số đối số, bao gồm dữ liệu được mã hóa, kích thước của mã QR và mức hiệu chỉnh lỗi.

Mã sau đây tạo mã QR mã hóa văn bản "Example Domain":

`` `Python
Từ QRCode Nhập QRCode

data = "Example Domain"

qrcode = qrcode (
phiên bản = 1,
Error_Correction = qrcode.error_correct_l,
box_size = 10,
Border = 4,
)

qrcode.add_data (dữ liệu)
qrcode.make ()

img = qrcode.to_image ()
img.save ("qrcode.png")
`` `

Mã này sẽ tạo mã QR trông như thế này:

[! [Mã QR] ( )] (Example Domain)

### Thêm logo vào mã QR

Bạn cũng có thể thêm logo vào mã QR.Để làm điều này, chúng ta có thể sử dụng hàm `add_logo ()`.Hàm này có một đường dẫn đến hình ảnh logo và kích thước của logo.

Mã sau đây thêm logo vào mã QR mà chúng tôi đã tạo trong phần trước:

`` `Python
Từ QRCode Nhập QRCode

data = "Example Domain"

qrcode = qrcode (
phiên bản = 1,
Error_Correction = qrcode.error_correct_l,
box_size = 10,
Border = 4,
)

qrcode.add_data (dữ liệu)
qrcode.make ()

img = qrcode.to_image ()
img.save ("qrcode.png")

qrcode.add_logo ("logo.png", size = 100)

img = qrcode.to_image ()
img.save ("qrcode_with_logo.png")
`` `

Mã này sẽ tạo mã QR trông như thế này:

[! [Mã QR có logo] ( )] (Example Domain)

### Lưu mã QR

Khi bạn đã tạo mã QR, bạn có thể lưu nó vào một tệp.Để làm điều này, bạn có thể sử dụng hàm `save ()`.Chức năng này có một đường dẫn đến tệp nơi bạn muốn lưu mã QR.

Mã sau đây lưu mã QR mà chúng tôi đã tạo trong phần trước cho một tệp có tên là "qrcode.png":

`` `Python
Từ QRCode Nhập QRCode

data = "Example Domain"

qrcode = qrcode (
phiên bản = 1,
Error_Correction = qrcode.error_correct_l,
box_size = 10,
Border = 4,
)

qrcode.add_data (dữ liệu)
qrcode.make ()

img = qrcode.to_image ()
img.save ("qrcode.png")
`` `

### Người giới thiệu

* [Tài liệu mã Pillow QR] (https://pillow.readthedocs.io/en/stable/reference/image.html#qrcode)
* [Cách tạo mã QR trong Python
=======================================
## How to Create a QR Code in Python

QR codes are a quick and easy way to share information. They can be used to store URLs, contact information, coupons, and more. In this tutorial, we will show you how to create a QR code in Python.

### Prerequisites

To follow along with this tutorial, you will need the following:

* A Python interpreter
* The [Pillow](https://pypi.org/project/Pillow/) library

### Creating a QR Code

To create a QR code, we will use the `qrcode` module from the Pillow library. The `qrcode` module provides a number of functions for creating and decoding QR codes.

To create a QR code, we can use the `QRCode()` function. This function takes a number of arguments, including the data to be encoded, the size of the QR code, and the error correction level.

The following code creates a QR code that encodes the text "Example Domain":

```python
from qrcode import QRCode

data = "Example Domain"

qrcode = QRCode(
version=1,
error_correction=qrcode.ERROR_CORRECT_L,
box_size=10,
border=4,
)

qrcode.add_data(data)
qrcode.make()

img = qrcode.to_image()
img.save("qrcode.png")
```

This code will create a QR code that looks like this:

[![QR Code](https://i.imgur.com/8222231.png)](https://www.example.com)

### Adding a Logo to a QR Code

You can also add a logo to a QR code. To do this, we can use the `add_logo()` function. This function takes a path to the logo image and the size of the logo.

The following code adds a logo to the QR code that we created in the previous section:

```python
from qrcode import QRCode

data = "Example Domain"

qrcode = QRCode(
version=1,
error_correction=qrcode.ERROR_CORRECT_L,
box_size=10,
border=4,
)

qrcode.add_data(data)
qrcode.make()

img = qrcode.to_image()
img.save("qrcode.png")

qrcode.add_logo("logo.png", size=100)

img = qrcode.to_image()
img.save("qrcode_with_logo.png")
```

This code will create a QR code that looks like this:

[![QR Code with Logo](https://i.imgur.com/6789012.png)](https://www.example.com)

### Saving a QR Code

Once you have created a QR code, you can save it to a file. To do this, you can use the `save()` function. This function takes a path to the file where you want to save the QR code.

The following code saves the QR code that we created in the previous section to a file called "qrcode.png":

```python
from qrcode import QRCode

data = "Example Domain"

qrcode = QRCode(
version=1,
error_correction=qrcode.ERROR_CORRECT_L,
box_size=10,
border=4,
)

qrcode.add_data(data)
qrcode.make()

img = qrcode.to_image()
img.save("qrcode.png")
```

### References

* [Pillow QR Code Documentation](https://pillow.readthedocs.io/en/stable/reference/Image.html#qrcode)
* [How to Create a QR Code in Python
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top