科学工具
科学工具让世界更美好
让世界更美好

图像分层分解和编辑工具 Qwen-Image-Layered

Qwen-Image-Layered 能将图像分解为多个RGBA图层,让图像可编辑,允许用户独立地操纵每个图层,支持高保真度的基础编辑操作,如重新着色、替换内容、修改文本、删除、调整大小和自由移动图层对象,避免对其他内容的干扰。Qwen-Image-Layered 提供图层数量选择,支持递归分解,能实现无限层次的精细化处理。Qwen-Image-Layered 还提供实用的Web界面,方便用户进行图像分解、将图层导出为可编辑的PPTX文件,结合Qwen-Image-Edit进行透明图像编辑。

Qwen-Image-Layered 安装和使用

环境要求

需要 transformers 的版本 ≥ 4.51.3(支持 Qwen2.5-VL),安装最新版本的 diffusers 和相关依赖。

pip install git+https://github.com/huggingface/diffusers
pip install python-pptx
pip install psd-tools

基础使用方法

from diffusers import QwenImageLayeredPipeline
import torch
from PIL import Image

pipeline = QwenImageLayeredPipeline.from_pretrained("Qwen/Qwen-Image-Layered")
pipeline = pipeline.to("cuda", torch.bfloat16)
pipeline.set_progress_bar_config(disable=None)

image = Image.open("asserts/test_images/1.png").convert("RGBA")
inputs = {
    "image": image,
    "generator": torch.Generator(device='cuda').manual_seed(777),
    "true_cfg_scale": 4.0,
    "negative_prompt": " ",
    "num_inference_steps": 50,
    "num_images_per_prompt": 1,
    "layers": 4,
    "resolution": 640,      # 可通过不同尺寸(640, 1024)确定分辨率,当前版本推荐 640
    "cfg_normalize": True,  # 是否启用 cfg 归一化
    "use_en_prompt": True,  # 用户未提供描述时,自动选择描述语言
}
with torch.inference_mode():
    output = pipeline(**inputs)
    output_image = output.images[0]

for i, image in enumerate(output_image):
    image.save(f"{i}.png")

部署 Qwen-Image-Layered

1、启动图像分解与导出界面

运行以下脚本,启动基于 Gradio 的网页界面,可分解图像并将图层导出为 pptx、zip 和 psd 文件,方便灵活编辑和移动图层:

python src/app.py

2、启动透明图像编辑工具

若需编辑分解后的特定图层,运行以下脚本启动 Gradio 网页界面,借助 Qwen-Image-Edit 编辑带透明度的图像:

python src/tool/edit_rgba_image.py

3、合并编辑后的图层

编辑完单个分解图层后,可通过以下脚本将其合并为新图像。注意按从底层到顶层的顺序上传图层:

python src/tool/combine_layers.py

Qwen-Image-Layered 功能

分层分解的实际应用

输入一张图像后,Qwen-Image-Layered 能将其分解为多个独立可编辑的 RGBA 图层。编辑操作仅作用于目标图层,与其他内容物理隔离,保障编辑效果的一致性。

• 改色:对第一个图层进行改色,其他内容保持不变。

• 替换元素:将第二个图层中的人物从女孩替换为男孩(需配合 Qwen-Image-Edit 使用)。

• 文本修改:将图层中的文本修改为“Qwen-Image”(需配合 Qwen-Image-Edit 使用)。

• 删除元素:干净移除不需要的对象。

• 缩放:不扭曲图像的前提下调整对象尺寸。

• 重新定位:在画布内自由移动对象位置。