Qwen-Image Documentation

repository·main·Indexed 27 days ago

https://github.com/qwenlm/qwen-image

A 20B MMDiT image foundation model for high-quality text-to-image generation and precise image editing. Features include Qwen-Image-2512 for high-fidelity generation with enhanced human realism and text rendering, and Qwen-Image-Edit-2511/2509 for advanced image manipulation, character consistency, and ControlNet support. The library integrates with diffusers, SGLang-Diffusion, and ModelScope, offering tools for prompt enhancement and multi-GPU API deployment.

Tokens
2.5K
Snippets
5
Records
13
Agent score
44%

What's inside Qwen-Image

  1. Capabilities of Qwen-Image-2512 (Text-to-Image)

    main

    Qwen-Image-2512 is optimized for high-fidelity text-to-image generation with the following key enhancements:

    • Enhanced Human Realism: Provides significantly richer facial details, better environmental context, and precise rendering of hair strands and skin textures (e.g., wrinkles for aged subjects).
    • Finer Natural Detail: Superior fidelity in rendering complex natural elements such as water flow, foliage, mist, and animal fur/textures.
    • Improved Text Rendering: High accuracy in text layout, typography, and multimodal integration, capable of generating complex infographics, PPT slides, and educational posters with clear, readable text.
  2. Capabilities of Qwen-Image-Edit-2511 (Image Editing)

    main

    Qwen-Image-Edit-2511 is designed for advanced image manipulation and editing with the following features:

    • Character Consistency: Performs imaginative edits on an input portrait while preserving the subject's identity and visual characteristics.
    • Multi-Person Consistency: Enables high-fidelity fusion of multiple separate person images into a single, coherent group shot.
    • Integrated LoRAs: Includes built-in support for popular community LoRAs, allowing for effects like realistic lighting enhancement and new viewpoint generation without extra tuning.
    • Industrial Design Support: Capable of batch industrial product design and material replacement for components.
    • Geometric Reasoning: Can generate auxiliary construction lines for design or annotation purposes.
  3. Community Ecosystem and Integrations for Qwen-Image

    main

    Qwen-Image is supported by several third-party platforms and tools:

    • Diffusers: Native support available; LoRA and finetuning workflows are in development.
    • ModelScope:
      • DiffSynth-Studio: Supports low-GPU-memory offloading (inference within 4GB VRAM), FP8 quantization, and LoRA/full training.
      • DiffSynth-Engine: Provides optimizations like FBCache-based acceleration and CFG parallel.
      • ModelScope AIGC Central: Provides web-based interfaces for Image Generation and LoRA Training.
    • WaveSpeedAI: Deployed on their platform for text-to-image generation.
    • LiblibAI: Offers native support for Qwen-Image models.
  4. Multi-image Editing with Qwen-Image-Edit-2509

    main

    Qwen-Image-Edit-2509 supports multi-image editing by utilizing image concatenation. It is optimized for inputs containing 1 to 3 images. Supported combinations include:

    • person + person
    • person + product
    • person + scene
    • person + object

    Multi-image input also allows for the use of ControlNet keypoint maps to control elements like a person's pose.

  5. Enhance prompts for Text-to-Image and Image Editing

    main

    To improve stability and quality, use the official Prompt Enhancement tools:

    For Text-to-Image: Integrate rewrite from tools.prompt_utils to optimize prompts and support multiple languages.

    from tools.prompt_utils import rewrite
    prompt = rewrite(prompt)

    Or run via CLI:

    DASHSCOPE_API_KEY=sk-xxx python examples/generate_w_prompt_enhance.py

    For Image Editing: Use polish_edit_prompt from tools.prompt_utils to improve editing stability.

    from tools.prompt_utils import polish_edit_prompt
    prompt = polish_edit_prompt(prompt, pil_image)
  6. Install Qwen-Image dependencies

    main

    To use Qwen-Image, ensure you have transformers>=4.51.3 (required for Qwen2.5-VL support) and install the latest version of diffusers directly from the GitHub repository.

    pip install git+https://github.com/huggingface/diffusers
  7. Single-image Consistency Improvements in Qwen-Image-Edit-2509

    main

    Qwen-Image-Edit-2509 provides enhanced identity and style preservation for single-image editing tasks:

    • Person Editing: Improved preservation of facial identity across various portrait styles, pose transformations, and old photo restoration. Supports generating cartoon characters and cultural creations.
    • Product Editing: Improved preservation of product identity, enabling tasks like generating product posters from plain-background images or creating logos.
    • Text Editing: Supports modifying text content as well as editing font type, font color, and font material. It also allows for precise text editing and seamless integration of text changes within image editing (e.g., poster editing).
  8. Deploy Qwen-Image Multi-GPU API Server

    main

    Deploy a Gradio-based web interface that supports multi-GPU parallel processing, queue management, and automatic prompt optimization. Configuration is handled via environment variables.

    Environment Variables:

    • NUM_GPUS_TO_USE: Number of GPUs to use.
    • TASK_QUEUE_SIZE: Task queue size.
    • TASK_TIMEOUT: Task timeout in seconds.

    Run Command:

    cd src
    DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxxxxxx python examples/demo.py
    export NUM_GPUS_TO_USE=4
    export TASK_QUEUE_SIZE=100
    export TASK_TIMEOUT=300
    
    cd src
    DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxxxxxx python examples/demo.py
  9. Edit images with Qwen-Image-Edit-2511

    main

    Use QwenImageEditPlusPipeline from diffusers for image editing. This version supports multiple image inputs and improved consistency. Pass a dictionary of inputs containing image (a list of PIL images), prompt, and other generation parameters to the pipeline.

    import os
    import torch
    from PIL import Image
    from diffusers import QwenImageEditPlusPipeline
    from io import BytesIO
    import requests
    
    pipeline = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2511", torch_dtype=torch.bfloat16)
    pipeline.to('cuda')
    
    image1 = Image.open(BytesIO(requests.get("https://.../edit2511input.png").content))
    prompt = "这个女生看着面前的电视屏幕,屏幕上面写着“阿里巴巴”"
    
    inputs = {
        "image": [image1],
        "prompt": prompt,
        "generator": torch.manual_seed(0),
        "true_cfg_scale": 4.0,
        "negative_prompt": " ",
        "num_inference_steps": 40,
        "guidance_scale": 1.0,
        "num_images_per_prompt": 1,
    }
    
    with torch.inference_mode():
        output = pipeline(**inputs)
        output_image = output.images[0]
        output_image.save("output_image_edit_2511.png")
  10. Use Qwen-Image-Edit-2511 with SGLang-Diffusion

    main

    You can use SGLang-Diffusion to run Qwen-Image-Edit-2511 for image editing tasks. The sglang generate command allows you to provide a model path, a text prompt, and multiple image paths to perform complex editing (e.g., combining elements from different images).

    sglang generate --model-path Qwen/Qwen-Image-Edit-2511 --prompt "make the girl in Figure 1 dance with the capybara in Figure 2."  --image-path "https://github.com/lm-sys/lm-sys.github.io/releases/download/test/TI2I_Qwen_Image_Edit_Input.jpg" "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/edit2509/edit2509_2.jpg"
  11. Generate images with Qwen-Image-2512

    main

    Use QwenImagePipeline from diffusers to perform text-to-image generation with improved character realism and texture. It is recommended to use prompt enhancing tools found in src/examples/tools/prompt_utils_2512.py for best results. You can specify different aspect ratios by providing width and height parameters.

    from diffusers import QwenImagePipeline
    import torch
    
    # Load the pipeline
    if torch.cuda.is_available():
        torch_dtype = torch.bfloat16
        device = "cuda"
    else:
        torch_dtype = torch.float32
        device = "cpu"
    
    pipe = QwenImagePipeline.from_pretrained("Qwen/Qwen-Image-2512", torch_dtype=torch_dtype).to(device)
    
    # Generate image
    prompt = 'A 20-year-old East Asian girl...' 
    negative_prompt = "低分辨率,低画质..."
    
    # Aspect ratio mapping example
    # "16:9": (1664, 928)
    width, height = (1664, 928)
    
    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        width=width,
        height=height,
        num_inference_steps=50,
        true_cfg_scale=4.0,
        generator=torch.Generator(device="cuda").manual_seed(42)
    ).images[0]
    
    image.save("example.png")