A minimal example showing how to:
- Generate images with the Qwen‑Image diffusion model (
qwen_image.py). - Edit existing images using the Qwen‑Image‑Edit pipeline (
qwen_image_edit.py). - Containerize the environment with Docker.
- Build and run the container via convenient
maketargets.
| Tool | Minimum Version |
|---|---|
| Docker | ≥ 20.10 |
| NVIDIA GPU with CUDA 12.9 drivers (optional but recommended) | |
make (installed on most Linux/macOS environments) |
|
git (to clone the repo) |
Note: If you run the scripts on a CPU‑only machine, the code will fall back to
torch.float32automatically.
📂 .
├── qwen_image.py # Text‑to‑image generation script
├── qwen_image_edit.py # Image‑inpainting / editing script
├── Dockerfile # Container definition
├── Makefile # Helper commands for build/run
├── .dockerignore # Files excluded from the Docker context
└── README.md # ← This file
make build # builds and tags the image with both latest and git‑SHA tagsThe Dockerfile installs PyTorch (CUDA 12.9), 🤗 Transformers, and Diffusers from source.
If you only need a local build (no Git metadata), you can run:
make build-localmake run # interactive shell with GPU accessThe container mounts:
- Your repository (
$(pwd)) to/root(so you can edit scripts live). - A local cache folder (
/tmp/hugginface) to avoid re‑downloading models.
Tip: Use
make run-localif you don’t have a CI environment or want to skip pulling the image again.
Inside the container (or on host if you have the required Python packages):
python qwen_image.pyThis script:
- Loads the
Qwen/Qwen-Imagemodel. - Generates an image with a preset prompt (feel free to edit
promptorpositive_magic). - Saves the output as
example.pngin the current directory.
You can change the aspect ratio by editing the aspect_ratios dictionary and adjusting width, height = aspect_ratios["16:9"].
Place an image named input.png in the repository root (or modify the path in the script), then run:
python qwen_image_edit.pyThe script:
- Loads
Qwen/Qwen-Image-Edit. - Applies the edit described in
prompt(e.g., “Change the rabbit's color to purple…”). - Saves the edited result as
output_image_edit.png.
- Prompts: Edit
promptstrings in either script to suit your use‑case. - Seed & CFG: Adjust
generator=torch.Generator(...).manual_seed(...)andtrue_cfg_scalefor deterministic or more creative outputs. - Model selection: Replace
model_name = "Qwen/Qwen-Image"with a different model identifier from Hugging Face Hub if desired.
To stop the container and remove any dangling images:
docker container prune # removes stopped containers
docker image prune -a # removes unused images (be careful!)This demo repository is provided as‑is for educational purposes. Refer to the individual model licenses on Hugging Face for usage restrictions.
Happy image generation! 🎨🚀