整体逻辑与设计思路
PhotoshopImageGenerator是一个基于Python和Win32COM的自动化工具,通过控制Adobe Photoshop CC 2019创建多样化的图像数据集。其核心设计思路是通过程序化调用Photoshop的图像编辑能力,为基础图像添加随机元素(图片、文本、形状)和效果,快速生成大量变体图像用于机器学习训练或数据增强。
核心工作流程
初始化连接:通过COM接口建立与Photoshop的连接文档管理:打开基础图像或创建新文档元素添加:随机插入图片、文本和形状效果应用:应用滤镜、变换和混合模式批量处理:自动化生成指定数量的变体并保存
类结构与关键方法解析
核心类:PhotoshopAutomation
初始化与连接管理
class PhotoshopAutomation:
def __init__(self):
self.ps_app = None
self.connected = False
self.connect_to_photoshop()
def connect_to_photoshop(self):
"""建立与Photoshop的连接"""
try:
self.ps_app = win32com.client.Dispatch("Photoshop.Application")
self.ps_app.Visible = True # 使Photoshop窗口可见
self.connected = True
print("成功连接到Photoshop")
return True
except Exception as e:
print(f"连接Photoshop失败: {e}")
self.connected = False
return False
def is_connected(self):
"""检查是否已连接到Photoshop"""
return self.connected
文档操作方法
: 创建新文档
create_document(width, height, resolution, color_mode)
: 打开现有图像文件
open_document(file_path)
: 保存文档到指定格式
save_document(document, output_path, file_format)
: 关闭文档
close_document(document, save_changes)
核心功能方法
generate_dataset: 主方法,协调整个数据集生成流程
def generate_dataset(self, base_image_path, output_dir, count=100, file_format='JPEG'):
"""为单张基础图片生成数据集"""
if not self.is_connected():
print("未连接到 Photoshop,无法生成数据集")
return False
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f"开始处理基础图片: {os.path.basename(base_image_path)},共生成{count}张图像...")
for i in range(count):
try:
# 打开基础图像
doc = self.open_document(base_image_path)
if not doc:
print(f"无法打开基础图像,跳过第 {i + 1} 张图像")
continue
# 应用随机变换
if random.random() > 0.3:
self.apply_random_effects(doc)
# 添加随机元素
if random.random() > 0.4:
self.add_random_image(doc)
if random.random() > 0.5:
self.add_random_text(doc)
# 保存图像
output_filename = f"image_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{i + 1}.{file_format.lower()}"
output_path = os.path.join(output_dir, output_filename)
self.save_document(doc, output_path, file_format)
# 关闭文档
self.close_document(doc, False)
if (i + 1) % 10 == 0:
print(f"已生成 {i + 1}/{count} 张图像")
except Exception as e:
print(f"生成第 {i + 1} 张图像时出错: {str(e)}")
continue
print(f"数据集生成完成,已保存到: {output_dir}")
return True
add_random_image: 添加随机图片并设置属性
def add_random_image(self, document, image_library_path='numpic'):
"""向文档中添加随机图片并应用随机效果"""
from PIL import Image
# 支持的图片扩展名
SUPPORTED_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.gif', '.bmp')
try:
# 检查图片库路径是否存在
if not os.path.exists(image_library_path):
print(f"警告: 图片库路径 '{image_library_path}' 不存在")
return False
# 获取图片库中所有支持的图片文件
image_files = []
for root, _, files in os.walk(image_library_path):
for file in files:
if file.lower().endswith(SUPPORTED_EXTENSIONS):
image_files.append(os.path.join(root, file))
if not image_files:
print(f"警告: 图片库路径 '{image_library_path}' 中未找到支持的图片文件")
return False
# 随机选择一张图片
selected_image = random.choice(image_files)
selected_image = os.path.abspath(selected_image)
# 打开图片作为临时文档
temp_doc = self.ps_app.Open(selected_image)
# 选择全部并复制
temp_doc.Selection.SelectAll()
temp_doc.Selection.Copy()
# 关闭临时文档
temp_doc.Close(2) # 2 = psDoNotSaveChanges
# 粘贴到目标文档
document.Paste()
# 获取当前活动图层(刚粘贴的图层)
placed_layer = document.ActiveLayer
# 应用随机不透明度 (50-100%)
opacity_value = random.randint(50, 100)
placed_layer.Opacity = opacity_value
print(f"设置图层不透明度: {opacity_value}%")
print(f"成功添加图片: {os.path.basename(selected_image)}")
return True
except Exception as e:
tb = traceback.extract_tb(e.__traceback__)
line_number = tb[-1].lineno if tb else "未知"
print(f"添加随机图片失败, 行号: {line_number}: {str(e)}")
return False
使用示例
以下是使用PhotoshopImageGenerator类生成图像数据集的完整示例:
def main():
"""主函数:处理目录中所有基础图像并生成数据集"""
# 配置参数
base_dir = "G: emppsPsImage" # 基础图像目录
output_dir = "G: emppsgenerated_dataset" # 输出目录
image_count = 10 # 每张基础图生成图像数量
file_format = "JPEG" # 输出格式
# 获取目录中所有图像文件的完整路径
if not os.path.isdir(base_dir):
print(f"目录不存在: {base_dir}")
return
image_extensions = ('.jpg', '.jpeg', '.png', '.psd')
base_image_paths = [
os.path.join(base_dir, f)
for f in os.listdir(base_dir)
if f.lower().endswith(image_extensions)
]
if not base_image_paths:
print(f"在目录 {base_dir} 中未找到图像文件")
return
# 创建Photoshop自动化对象
ps = PhotoshopAutomation()
if not ps.is_connected():
return
# 遍历所有基础图像生成数据集
for base_image_path in base_image_paths:
ps.generate_dataset(base_image_path, output_dir, image_count, file_format)
time.sleep(1) # 短暂等待避免资源冲突
print("所有基础图像的数据集生成完成!")
if __name__ == "__main__":
main()
关键技术点
Win32COM接口:通过
实现Python与Photoshop的通信随机化策略:使用概率阈值控制不同元素的添加几率,确保数据集多样性增加更多图像变换效果(旋转、缩放、扭曲等)实现更精细的图层控制(位置、大小、混合模式)添加数据增强参数配置文件,支持自定义生成策略集成图像标注功能,直接生成带标签的数据集
win32com.client.Dispatch("Photoshop.Application")
错误处理:完善的异常捕获机制,确保批量处理过程中单个错误不影响整体流程资源管理:严格的文档打开/关闭流程,避免Photoshop资源泄漏