You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pillow Image.paste实现服装logo批量贴附并支持Photoshop调整

电商服装图片批量贴Logo的自动化优化方案

问题背景

我需要为电商平台实现服装产品图片的Logo批量贴附自动化处理,新品图片大多对齐规范,降低了自动化实施难度。目前用Pillow编写了固定坐标贴Logo的代码,但卫衣、夹克等服装与T恤、Polo衫的Logo对齐方式存在差异,容易出现贴附错误。想请教:

  1. 如何导出带图层的图片以便在Photoshop等软件中手动修正错误?
  2. 有没有其他更高效的解决方案?

当前使用的Pillow代码:

from PIL import Image
import os

# Path to the folder containing product images
folder_path = "D:/"
save_path = "D:/"

# Path to the logo image
logo_path = "D:/x.png"
logo = Image.open(logo_path)
logo_width, logo_height = logo.size

# Iterate through each file in the folder
for filename in os.listdir(folder_path):
    # if filename.endswith(".png"):  # Assuming all images are PNG, you can modify accordingly
        # Load the product image
        product_image_path = os.path.join(folder_path, filename)
        product_image = Image.open(product_image_path)

        # Define where logo is placed on product image
        x, y = 618, 245
        region = (x, y, x + logo_width, y + logo_height)

        # Place logo on product image
        product_image.paste(logo, region, logo)

        # Save or display the modified image (choose one or both)
        # Save the modified image
        product_image.save(os.path.join(save_path, f"modified_{filename}"))

        # Display the modified image (optional)
        # product_image.show()

解决方案一:导出带独立图层的PSD文件

Pillow原生无法直接保存带独立图层的文件,推荐使用psd-tools库实现,导出的PSD文件可在Photoshop中单独编辑Logo图层:

  1. 安装依赖
pip install psd-tools
  1. 修改后的代码
from psd_tools import PSDImage, Layer
from PIL import Image
import os

folder_path = "D:/"
save_path = "D:/"
logo_path = "D:/x.png"
logo = Image.open(logo_path).convert("RGBA")
logo_width, logo_height = logo.size

for filename in os.listdir(folder_path):
    if filename.lower().endswith((".png", ".jpg", ".jpeg")):
        product_img_path = os.path.join(folder_path, filename)
        product_img = Image.open(product_img_path).convert("RGBA")
        
        # 创建PSD文件,以产品图为背景图层
        psd = PSDImage.new(product_img.size, mode="RGBA")
        bg_layer = Layer.from_image(product_img)
        psd.append(bg_layer)
        
        # 添加独立的Logo图层,设置对应坐标
        logo_layer = Layer.from_image(logo)
        logo_layer.offset = (618, 245)  # 替换为原代码的固定坐标
        psd.append(logo_layer)
        
        # 保存可编辑的PSD文件
        psd.save(os.path.join(save_path, f"editable_{os.path.splitext(filename)[0]}.psd"))

解决方案二:按品类优化自动化定位

针对不同服装品类的位置差异,通过分类批量处理减少错误:

  • 提前按品类分文件夹:将T恤、卫衣、夹克等图片分别放入对应文件夹,为每个品类设置专属坐标
from PIL import Image
import os

folder_path = "D:/"
save_path = "D:/"
logo_path = "D:/x.png"
logo = Image.open(logo_path)
logo_width, logo_height = logo.size

# 定义各品类的Logo坐标
category_coords = {
    "t_shirt": (618, 245),
    "hoodie": (580, 260),
    "jacket": (595, 250)
}

# 遍历每个品类文件夹处理
for category, (x, y) in category_coords.items():
    category_folder = os.path.join(folder_path, category)
    if not os.path.isdir(category_folder):
        continue
    for filename in os.listdir(category_folder):
        if filename.lower().endswith((".png", ".jpg", ".jpeg")):
            product_img_path = os.path.join(category_folder, filename)
            product_img = Image.open(product_img_path)
            region = (x, y, x + logo_width, y + logo_height)
            product_img.paste(logo, region, logo)
            product_img.save(os.path.join(save_path, f"{category}_modified_{filename}"))
  • 极简图像识别辅助:如果不想手动分类,可借助OpenCV检测服装领口、下摆等特征点,计算Logo的相对位置(适合版型统一的新品图片)。

解决方案三:批量处理+少量手动修正混合模式

如果仅少数图片出现位置错误,可采用此方案提升效率:

  1. 用原代码批量处理所有图片,手动筛选出位置错误的图片并记录文件名
  2. 对错误图片,单独用方案一的代码导出PSD文件,或直接在Photoshop中打开原图+Logo快速调整位置

内容的提问来源于stack exchange,提问作者leonj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 17:09:58