Generating QR codes on the fly is essential for mobile payments, event tickets, and inventory tracking. The Aspose.BarCode Cloud SDK for Python lets you create high‑quality QR codes directly from your Python applications. In this guide we will walk through the Aspose.BarCode cloud Python QR code workflow, from setting up credentials to customizing colors and embedding logos. You’ll also learn how to batch‑generate multiple codes and use equivalent cURL commands for REST integration. The Aspose.BarCode cloud Python QR code library also supports adding logos and adjusting error correction levels for robust scanning.
Before You Start: Prerequisites and Installation
Before you begin, ensure you have the following:
- Python 3.7 or higher installed on your development machine.
- An Aspose Cloud account with client_id and client_secret values.
- Access to a folder where the generated PNG files will be saved.
Install the SDK via pip:
pip install aspose-barcode-cloud
Download the latest package and view release notes on the download page.
Next, configure the API client with your credentials (excerpt from the full script):
from asposebarcodecloud import BarCodeApi, ApiClient, Configuration
API_CLIENT_ID = "YOUR_CLIENT_ID"
API_CLIENT_SECRET = "YOUR_CLIENT_SECRET"
config = Configuration()
config.host = "https://api.aspose.cloud"
config.api_key['client_id'] = API_CLIENT_ID
config.api_key['client_secret'] = API_CLIENT_SECRET
barcode_api = BarCodeApi(ApiClient(config))
With the SDK installed and credentials set, you are ready to generate QR codes.
Step-by-Step Guide to Generate Custom QR Codes with Aspose.BarCode Cloud SDK for Python
This step‑by‑step guide demonstrates how to work with the Aspose.BarCode cloud Python QR code API to produce fully customized QR images.
Step 1: Load the Source Document and Set Credentials
First, import the required modules and create the BarCodeApi instance as shown above. This prepares the library to communicate with the Aspose cloud service.
Step 2: Define QR Code Parameters
Create a dictionary that holds the QR code options such as type, text, format, colors, and error correction level.
params = {
"type": "QR",
"text": "https://example.com",
"format": "png",
"foreColor": "Black",
"backColor": "White",
"qrErrorLevel": "LevelQ"
}
For more details on each parameter, see the BarCode API reference.
Step 3: Embed an Optional Logo
If you want to add a logo, read the image file, convert it to Base64, and add it to the parameters.
def read_logo_as_base64(logo_path: str) -> str:
with open(logo_path, "rb") as logo_file:
return base64.b64encode(logo_file.read()).decode('utf-8')
if logo_path and os.path.isfile(logo_path):
params["logo"] = read_logo_as_base64(logo_path)
This step demonstrates the generate QR code with logo Aspose.BarCode sdk capability.
Step 4: Generate and Save the QR Code
Call the get_barcode_generate method and write the returned bytes to a file.
result_bytes = barcode_api.get_barcode_generate(**params)
with open(output_path, "wb") as out_file:
out_file.write(result_bytes)
print(f"QR code saved to: {output_path}")
Step 5: Batch Generate Multiple QR Codes (Optional)
Loop through a list of texts and filenames to create several QR codes in one run.
qr_items = [
{"text": "https://example.com/product/1", "filename": "qr_product_1.png"},
{"text": "Contact: +1-800-555-0199", "filename": "qr_contact.png"}
]
for item in qr_items:
output_file = os.path.join("generated_qrcodes", item["filename"])
generate_qr_code(text=item["text"], output_path=output_file, **common_settings)
This illustrates batch generate QR codes Python Aspose in a concise loop.
Complete Code Example: Batch QR Code Generation with Custom Settings
The following script puts all the pieces together and demonstrates the Aspose.BarCode cloud Python QR code generation process with batch support.
import os
import base64
from asposebarcodecloud import BarCodeApi, ApiClient, Configuration, ApiException
# -------------------- Configuration --------------------
# Replace with your actual Aspose Cloud credentials
API_CLIENT_ID = "YOUR_CLIENT_ID"
API_CLIENT_SECRET = "YOUR_CLIENT_SECRET"
config = Configuration()
config.host = "https://api.aspose.cloud"
config.api_key['client_id'] = API_CLIENT_ID
config.api_key['client_secret'] = API_CLIENT_SECRET
# Initialize API instance
barcode_api = BarCodeApi(ApiClient(config))
# -------------------- Helper Functions --------------------
def read_logo_as_base64(logo_path: str) -> str:
"""Read an image file and return its Base64 representation."""
with open(logo_path, "rb") as logo_file:
return base64.b64encode(logo_file.read()).decode('utf-8')
def generate_qr_code(
text: str,
output_path: str,
fore_color: str = "Black",
back_color: str = "White",
error_correction: str = "LevelQ", # Options: LevelL, LevelM, LevelQ, LevelH
logo_path: str = None,
image_format: str = "png"
) -> None:
"""
Generates a QR code with optional customizations and saves it to disk.
"""
try:
# Prepare optional parameters
params = {
"type": "QR",
"text": text,
"format": image_format,
"foreColor": fore_color,
"backColor": back_color,
"qrErrorLevel": error_correction
}
# If a logo is supplied, embed it as a Base64 string
if logo_path and os.path.isfile(logo_path):
params["logo"] = read_logo_as_base64(logo_path)
# Call the Aspose.BarCode Cloud API
result_bytes = barcode_api.get_barcode_generate(**params)
# Write the resulting image to the specified file
with open(output_path, "wb") as out_file:
out_file.write(result_bytes)
print(f"QR code saved to: {output_path}")
except ApiException as e:
print(f"API Exception when generating QR for '{text}': {e}")
# -------------------- Batch Generation Example --------------------
def batch_generate_qr_codes():
"""
Generates multiple QR codes in a loop with varying texts and customizations.
"""
# Sample data for batch processing
qr_items = [
{"text": "https://example.com/product/1", "filename": "qr_product_1.png"},
{"text": "https://example.com/product/2", "filename": "qr_product_2.png"},
{"text": "https://example.com/product/3", "filename": "qr_product_3.png"},
{"text": "Contact: +1-800-555-0199", "filename": "qr_contact.png"},
{"text": "Wi‑Fi: MyNetwork;P:password123;;", "filename": "qr_wifi.png"}
]
# Common customization settings
fore_color = "DarkBlue"
back_color = "LightYellow"
error_correction = "LevelH"
logo_path = "logo.png" # Ensure this file exists in the working directory
output_dir = "generated_qrcodes"
os.makedirs(output_dir, exist_ok=True)
for item in qr_items:
output_file = os.path.join(output_dir, item["filename"])
generate_qr_code(
text=item["text"],
output_path=output_file,
fore_color=fore_color,
back_color=back_color,
error_correction=error_correction,
logo_path=logo_path,
image_format="png"
)
# -------------------- Execution --------------------
if __name__ == "__main__":
batch_generate_qr_codes()
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Generating QR Codes via REST API with cURL
If you prefer a pure REST approach, you can use the Aspose.BarCode cloud Python QR code endpoint with cURL commands to achieve the same result.
1. Obtain an Access Token
curl -X POST "https://api.aspose.cloud/connect/token" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
The response contains an access_token that you will use in subsequent calls.
2. Generate a QR Code
Replace YOUR_ACCESS_TOKEN and adjust the query parameters as needed.
curl -X GET "https://api.aspose.cloud/v3.0/barcode/generate?type=QR&text=HelloWorld&format=png&foreColor=DarkBlue&backColor=LightYellow&qrErrorLevel=LevelH" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o qr_helloworld.png
To embed a logo, first convert the image to Base64 and pass it via the logo parameter.
3. Batch Generation (Loop Example)
You can script a loop in Bash or PowerShell that iterates over a list of texts and calls the above GET request for each entry, saving the results with distinct filenames.
For full API details, consult the official API documentation.
Customization Options: Colors, Logo, and Error Correction
The SDK exposes several parameters that let you tailor the appearance and resilience of the QR code.
- foreColor - Sets the foreground (module) color. Example:
"DarkBlue". - backColor - Sets the background color. Example:
"LightYellow". - qrErrorLevel - Controls error correction. Options are
LevelL,LevelM,LevelQ,LevelH. Higher levels increase robustness at the cost of data capacity. - logo - A Base64‑encoded image that will be placed at the center of the QR code.
Below is a snippet showing how these options are assembled in the request dictionary:
params = {
"type": "QR",
"text": "https://example.com",
"format": "png",
"foreColor": "DarkBlue",
"backColor": "LightYellow",
"qrErrorLevel": "LevelH",
"logo": read_logo_as_base64("logo.png") # optional
}
Adjust the values to match your branding guidelines or visual design requirements.
Questions You Might Have
-
How can I create custom QR code using Aspose.BarCode Python without writing a lot of code?
The SDK abstracts the heavy lifting. By setting a few parameters in theparamsdictionary and callingget_barcode_generate, you get a ready‑to‑use QR image. -
Is it possible to generate QR code with logo Aspose.BarCode SDK on a serverless platform?
Yes. The library works in any Python environment, including AWS Lambda or Azure Functions, as long as the runtime can reachhttps://api.aspose.cloud. -
What does the python script for QR code generation Aspose return?
Theget_barcode_generatemethod returns the raw image bytes, which you can write directly to a file or stream to a web response. -
How do I customize QR code colors Aspose.BarCode Python for my brand palette?
Use theforeColorandbackColorparameters with any HTML color name or HEX value supported by the API. -
What are the licensing options and how can I obtain a temporary license?
Full licenses are available for purchase on the Aspose website. For evaluation, you can request a temporary license from the temporary license page.