Embedded_game/chepai/合成车牌.py
2025-01-02 12:48:11 +08:00

47 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PIL import Image, ImageOps, ImageDraw, ImageFont, ImageFilter, ImageEnhance, ImageChops, ImageColor, ImageStat
#from PIL.Image import Resampling
from PIL.Image import Resampling#这里报错,可以运行
import random
import os
# D:\Waste\jzp\shengchengtupian\shengcheng\data
save_path = r"/Users/zhenzhipeng/development/数据集相关文件/车牌/黄底车牌(上下)" # 保存地址
background_plate = r"/Users/zhenzhipeng/development/数据集相关文件/背景" # 背景地址
license_plate = r"/Users/zhenzhipeng/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/abb33b2d6c9d8decd9ef7c0ddebe8cc2/Message/MessageTemp/db4557f50b0cffe1f5bebe69176fd814/File/motor_plate" # 车牌地址
background_list = [os.path.join(background_plate, f) for f in os.listdir(background_plate) if f.endswith('.jpg')]
license_list = [os.path.join(license_plate, f) for f in os.listdir(license_plate) if f.endswith('.jpg')]
print(background_list)
print(license_list)
for i in range(len(license_list)):
# 读取背景图像和车牌图像
background = Image.open(background_list[random.randint(0, len(background_list) - 1)])
license_plate = Image.open(license_list[i])
background = background.resize((800, 480))
# 将车牌图像转换为 RGBA 模式
license_plate = license_plate.convert('RGBA')
min_scale = max(background.width, background.height) / 3 / max(license_plate.width, license_plate.height)
# print(min_scale)
scale = min_scale
license_plate = license_plate.resize((int(license_plate.width * scale), int(license_plate.height * scale)))
# 生成随机的旋转角度
angle = random.randint(-80, 80)
# 将车牌图像旋转随机角度
rotated_license_plate = license_plate.rotate(angle, resample=Resampling.BICUBIC, expand=True)
# 生成随机的坐标位置
x = random.randint(0, background.width - license_plate.width - 10)
y = random.randint(0, background.height - license_plate.width - 10)
# 将车牌图像添加到背景图像的随机位置上
background.paste(rotated_license_plate, (x, y), rotated_license_plate)
# 保存合成后的图像为新的文件
background.save(save_path + '/result{}.jpg'.format(i+0)) # 先设0跑完看多少再加