下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:9828
这段代码实现了一个完整的拼多多订单截图生成器,包含了订单数据生成、图像处理和二维码生成等功能。代码使用了Pillow库进行图像处理,qrcode库生成二维码。请注意这仅用于学习Python编程技术,切勿用于任何非法用途。使用时需要准备一个中文字体文件(simhei.ttf)。
import os
import random
from datetime import datetime, timedelta
from PIL import Image, ImageDraw, ImageFont
import textwrap
import qrcode
import numpy as np
class PDDOrderGenerator:
def init(self):
self.template_path = "template.jpg" # 基础模板路径
self.output_dir = "output"
self.font_path = "simhei.ttf"
self.products = [
{"name": "苹果iPhone 15 Pro Max", "price": 8999, "category": "手机"},
{"name": "小米智能电视65英寸", "price": 3299, "category": "家电"},
{"name": "华为MateBook 14", "price": 5999, "category": "电脑"},
{"name": "耐克运动鞋", "price": 499, "category": "服饰"},
{"name": "三只松鼠零食大礼包", "price": 129, "category": "食品"}
]
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
def generate_random_order(self):
"""生成随机订单数据"""
product = random.choice(self.products)
order_time = datetime.now() - timedelta(days=random.randint(0, 30))
return {
"order_id": f"PDD{random.randint(1000000000, 9999999999)}",
"product_name": product["name"],
"price": product["price"],
"quantity": random.randint(1, 3),
"total_price": product["price"] * random.randint(1, 3),
"order_time": order_time.strftime("%Y-%m-%d %H:%M:%S"),
"receiver": random.choice(["张三", "李四", "王五", "赵六"]),
"address": random.choice([
"北京市朝阳区建国路88号",
"上海市浦东新区陆家嘴环路1000号",
"广州市天河区珠江新城",
"深圳市南山区科技园"
]),
"status": random.choice(["待发货", "已发货", "已完成"]),
"coupon": random.randint(0, 50),
"payment_method": random.choice(["微信支付", "支付宝", "银行卡"])
}
def add_text_to_image(self, img, text, position, font_size=20, color=(0, 0, 0)):
"""在图片上添加文字"""
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(self.font_path, font_size)
draw.text(position, text, font=font, fill=color)
return img
def add_qrcode(self, img, data, position, size=100):
"""添加二维码"""
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)
qr_img = qr.make_image(fill_color="black", back_color="white")
qr_img = qr_img.resize((size, size))
img.paste(qr_img, position)
return img
def generate_order_image(self, order_data):
"""生成订单图片"""
# 创建空白图片作为模板
img = Image.new("RGB", (750, 1334), (255, 255, 255))
draw = ImageDraw.Draw(img)
# 添加顶部拼多多LOGO区域
draw.rectangle([(0, 0), (750, 100)], fill=(255, 50, 50))
self.add_text_to_image(img, "拼多多", (300, 30), 40, (255, 255, 255))
# 添加订单信息
y_position = 120
self.add_text_to_image(img, f"订单编号: {order_data['order_id']}", (20, y_position), 24)
y_position += 40
self.add_text_to_image(img, f"下单时间: {order_data['order_time']}", (20, y_position), 24)
y_position += 40
# 添加商品信息
draw.rectangle([(10, y_position), (740, y_position + 120)], outline=(200, 200, 200), width=1)
self.add_text_to_image(img, "商品信息", (20, y_position + 10), 24)
y_position += 40
product_lines = textwrap.wrap(order_data["product_name"], width=30)
for line in product_lines:
self.add_text_to_image(img, line, (30, y_position), 20)
y_position += 30
self.add_text_to_image(img, f"单价: ¥{order_data['price']}", (30, y_position), 20)
self.add_text_to_image(img, f"数量: {order_data['quantity']}", (300, y_position), 20)
y_position += 30
self.add_text_to_image(img, f"小计: ¥{order_data['total_price']}", (30, y_position), 20)
y_position += 50
# 添加收货信息
draw.rectangle([(10, y_position), (740, y_position + 120)], outline=(200, 200, 200), width=1)
self.add_text_to_image(img, "收货信息", (20, y_position + 10), 24)
y_position += 40
self.add_text_to_image(img, f"收货人: {order_data['receiver']}", (30, y_position), 20)
y_position += 30
self.add_text_to_image(img, f"收货地址: {order_data['address']}", (30, y_position), 20)
y_position += 50
# 添加支付信息
draw.rectangle([(10, y_position), (740, y_position + 100)], outline=(200, 200, 200), width=1)
self.add_text_to_image(img, "支付信息", (20, y_position + 10), 24)
y_position += 40
self.add_text_to_image(img, f"支付方式: {order_data['payment_method']}", (30, y_position), 20)
y_position += 30
self.add_text_to_image(img, f"优惠券: -¥{order_data['coupon']}", (30, y_position), 20)
y_position += 30
self.add_text_to_image(img, f"实付款: ¥{order_data['total_price'] - order_data['coupon']}", (30, y_position), 24, (255, 50, 50))
y_position += 50
# 添加订单状态
self.add_text_to_image(img, f"订单状态: {order_data['status']}", (20, y_position), 24, (255, 50, 50))
y_position += 40
# 添加二维码
self.add_qrcode(img, f"https://mobile.yangkeduo.com/order.html?order_id={order_data['order_id']}", (550, y_position))
# 添加底部信息
self.add_text_to_image(img, "感谢您使用拼多多购物", (200, 1250), 20, (150, 150, 150))
# 保存图片
output_path = os.path.join(self.output_dir, f"{order_data['order_id']}.jpg")
img.save(output_path)
return output_path
def run(self, count=1):
"""生成指定数量的订单图片"""
for i in range(count):
order_data = self.generate_random_order()
output_path = self.generate_order_image(order_data)
print(f"已生成订单截图: {output_path}")
if name == "main":
generator = PDDOrderGenerator()
generator.run(5) # 生成5张订单截图