import qrcode from PIL import Image, ImageDraw, ImageFont import requests from io import BytesIO def generate_ticket_qr(ticket_info, filename): # Create QR code instance qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) # Add ticket information to the QR code qr.add_data(ticket_info) qr.make(fit=True) # Create an image from the QR Code instance img = qr.make_image(fill='black', back_color='white') # Save the QR code as an image file img.save(filename) def create_ticket_image(ticket_info, qr_filename, output_filename): # Create a blank ticket image ticket_img = Image.new('RGB', (400, 200), color='white') draw = ImageDraw.Draw(ticket_img) # Add ticket details to the image font = ImageFont.load_default() draw.text((10, 10), f"Passenger: {ticket_info['passenger_name']}", font=font, fill='black') draw.text((10, 30), f"From: {ticket_info['from']}", font=font, fill='black') draw.text((10, 50), f"To: {ticket_info['to']}", font=font, fill='black') draw.text((10, 70), f"Date: {ticket_info['date']}", font=font, fill='black') draw.text((10, 90), f"Ticket ID: {ticket_info['ticket_id']}", font=font, fill='black') # Load the QR code image qr_img = Image.open(qr_filename) qr_img = qr_img.resize((100, 100)) # Paste the QR code onto the ticket image ticket_img.paste(qr_img, (280, 50)) # Save the final ticket image ticket_img.save(output_filename) # Example usage ticket_info = { "passenger_name": "John Doe", "from": "City A", "to": "City B", "date": "2024-07-17", "ticket_id": "1234567890" } qr_filename = "ticket_qr.png" output_filename = "ticket.png" # Generate QR code generate_ticket_qr(ticket_info, qr_filename) # Create ticket image with QR code create_ticket_image(ticket_info, qr_filename, output_filename) # Explanation: # • Install Required Libraries: The qrcode library is used for generating QR codes, and Pillow (PIL) is used for image manipulation. # • Generate QR Code: The generate_ticket_qr function creates a QR code from the ticket information and saves it as an image. # • Create Ticket Image: The create_ticket_image function creates a ticket image, adds the ticket details, and pastes the QR code onto the ticket. # This script will generate a ticket image (ticket.png) with the passenger details and a QR code that can be scanned to retrieve the ticket information. # Feel free to customize the script to fit your specific requirements. If you have any questions or need further assistance, let me know! from PIL import Image from pyzbar.pyzbar import decode def read_qr_code(image_path): # Open the image using Pillow img = Image.open(image_path) # Decode the QR code using pyzbar decoded_objects = decode(img) if not decoded_objects: return "No QR code found in the image" # Print the QR code data for obj in decoded_objects: print("Type:", obj.type) print("Data:", obj.data.decode("utf-8")) return obj.data.decode("utf-8") # Example usage: qr_code_data = read_qr_code('ticket_qr.png') print("QR Code Data:", qr_code_data) def read_qr_code_from_url(url): # Fetch the image from the URL response = requests.get(url) if response.status_code == 200: # Load the image from the response content img = Image.open(BytesIO(response.content)) # Decode the QR code using pyzbar decoded_objects = decode(img) if not decoded_objects: return "No QR code found in the image" # Return the QR code data for obj in decoded_objects: return obj.data.decode("utf-8") else: return f"Failed to fetch image, status code: {response.status_code}" # Example usage: url = 'http://127.0.0.1:8000/qrcode_app/qrcodes1/13/' qr_code_data = read_qr_code_from_url(url) print("url QR Code Data:", qr_code_data)