下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:9981
在电商开发、测试及教学演示场景中,常需要生成逼真的虚拟订单截图。本文将通过Python+Pillow+Faker库实现自动化生成,包含完整订单号、商品信息、价格、时间戳等要素。
- 完整实现代码
from PIL import Image, ImageDraw, ImageFont from faker import Faker import random import datetime import qrcode import textwrap def generate_order_screenshot(): # 初始化伪造数据生成器 fake = Faker('zh_CN') # 1. 创建底图模板 img = Image.new('RGB', (800, 1200), color=(250, 250, 250)) draw = ImageDraw.Draw(img) # 2. 加载字体(实际使用需替换为本地字体路径) try: font_large = ImageFont.truetype("msyh.ttc", 24) font_medium = ImageFont.truetype("msyh.ttc", 18) font_small = ImageFont.truetype("msyh.ttc", 14) except: font_large = ImageFont.load_default() font_medium = ImageFont.load_default() font_small = ImageFont.load_default() # 3. 生成订单头部信息 order_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") order_no = f"DD{random.randint(1000000000,9999999999)}" draw.text((50, 40), "XX电商平台订单详情", fill=(0, 0, 0), font=font_large) draw.text((50, 80), f"订单编号: {order_no}", fill=(100, 100, 100), font=font_medium) draw.text((50, 110), f"下单时间: {order_time}", fill=(100, 100, 100), font=font_medium) # 4. 生成商品信息(随机3-5个商品) products = [] for i in range(random.randint(3,5)): product = { 'name': fake.text(max_nb_chars=20)[:-1], 'price': round(random.uniform(50, 500), 2), 'count': random.randint(1,3), 'sku': f"SPU{random.randint(10000,99999)}" } products.append(product) # 5. 绘制商品列表 y_position = 180 for idx, prod in enumerate(products): # 商品名称(自动换行处理) name_lines = textwrap.wrap(prod['name'], width=20) for line in name_lines: draw.text((50, y_position), line, fill=(0, 0, 0), font=font_medium) y_position += 25 # 商品其他信息 draw.text((50, y_position), f"SKU编码: {prod['sku']}", fill=(150, 150, 150), font=font_small) draw.text((600, y_position), f"×{prod['count']}", fill=(0, 0, 0), font=font_medium) y_position += 30 draw.text((550, y_position), f"¥{prod['price']:.2f}", fill=(255, 0, 0), font=font_medium) y_position += 40 # 分隔线 if idx < len(products)-1: draw.line([(30, y_position), (770, y_position)], fill=(200, 200, 200), width=1) y_position += 20 # 6. 生成金额汇总 total = sum(p['price']*p['count'] for p in products) y_position += 20 draw.text((550, y_position), "商品合计:", fill=(100, 100, 100), font=font_medium) draw.text((650, y_position), f"¥{total:.2f}", fill=(255, 0, 0), font=font_medium) y_position += 30 draw.text((550, y_position), "运费:", fill=(100, 100, 100), font=font_medium) draw.text((650, y_position), "¥0.00", fill=(0, 0, 0), font=font_medium) y_position += 30 draw.text((550, y_position), "实付款:", fill=(100, 100, 100), font=font_large) draw.text((650, y_position), f"¥{total:.2f}", fill=(255, 0, 0), font=font_large) # 7. 生成二维码 qr = qrcode.QRCode(version=1, box_size=4, border=2) qr.add_data(f"order://{order_no}") qr.make(fit=True) qr_img = qr.make_image(fill_color="black", back_color="white") img.paste(qr_img, (50, yposition+50)) # 8. 保存输出 img.save(f"order{order_no}.png") return img # 执行生成 if name == "main": generate_order_screenshot()
- 代码关键点说明
数据伪造:使用Faker库生成中文商品名、价格等随机数据
图像处理:Pillow库实现文字渲染、图形绘制和图像合成
布局算法:动态计算每个元素的位置坐标,实现自适应排版
细节处理:
商品名称自动换行
金额保留两位小数
时间戳动态生成
带品牌标识的二维码生成
- 扩展建议
可增加数据库支持,批量生成历史订单
添加物流信息模块
支持自定义模板皮肤
开发Web服务接口