New Tool Radar

Local AI

How to Generate an Image on Apple Silicon with MLX Stable Diffusion

Run the MLX Stable Diffusion example on your Mac, type a prompt, and save the generated image — with a quantization option that lets it run on an 8 GB Mac.

Schematic diagram with three steps: install the Stable Diffusion example requirements, run txt2image.py with a prompt to generate images, and save the result as a PNG file.

MLX is Apple’s machine learning framework for Apple Silicon, and the mlx-examples repository includes a Stable Diffusion implementation ported from Hugging Face’s diffusers, with model weights downloaded directly from the Hugging Face hub. It turns a text prompt into an image entirely on your Mac — no cloud service, no upload. This guide is about generating images; the MLX text guides cover language models, not pictures.

Install the example

Clone the mlx-examples repository and install the Stable Diffusion example’s dependencies. The page lists them as huggingface-hub to download the checkpoints, regex for tokenization, and tqdm, PIL, and numpy for the txt2image.py script:

git clone https://github.com/ml-explore/mlx-examples.git
cd mlx-examples/stable_diffusion
pip install -r requirements.txt

The implementation supports two models, stabilityai/sdxl-turbo and stabilityai/stable-diffusion-2-1, and the script’s --model argument selects between sdxl (the default) and sd.

Generate an image from a prompt

The fastest way to start is the txt2image.py script. The page’s own example prompt is:

python txt2image.py "A photo of an astronaut riding a horse on Mars." --n_images 4 --n_rows 2

That asks for 4 images arranged in 2 rows. The weights are downloaded from the Hugging Face hub on the first run, so the first generation takes longer than the ones after it. For a practice check, use a prompt with something countable in it, such as “A painting of a vase on a wooden table, dark background, still life.” — then look at the image and count the vase: there should be one vase on one table.

Save the result, and quantize on a small Mac

Use --output to choose the file name. On a memory-constrained Mac, add the -q flag: the script then quantizes the text encoder models to 4 bits and the UNet to 8 bits. The page says this allows generating images on an 8 GB Mac Mini with no swapping, and gives this exact command:

python txt2image.py --n_images 4 -q -v --output still-life.png "A painting of a vase on a wooden table, dark background, still life."

There is also an image-to-image script, image2image.py, which starts from an existing image instead of a prompt. It takes a strength value: 0.0 means no noise and 1.0 means starting from completely random noise, and it automatically downsamples the input image so its dimensions are divisible by 64.

Check What you should see If you do not see it
Installed pip install -r requirements.txt finishes in the stable_diffusion folder Check you are in the stable_diffusion folder of the cloned repo
Generated txt2image.py with a prompt produces images The first run downloads the weights — wait for the download to finish
Counted 4 images when you pass --n_images 4, arranged in 2 rows with --n_rows 2 Check the flags are spelled --n_images and --n_rows
Saved A PNG file appears at the path given with --output On an 8 GB Mac, add -q — the page says quantization is practically necessary there

Expected result. We checked the mlx-examples Stable Diffusion page for the supported models, the requirements, the txt2image.py example with –n_images 4 and –n_rows 2, the –model choices sdxl and sd, the -q quantization to 4-bit text encoders and 8-bit UNet, the 8 GB Mac Mini note, and the image2image.py strength range. We did not run the example or generate any image.

Limits

This is a local image generator, not a photo editor: the prompt decides the picture, and the same prompt can give a different image on another run. The default model is SDXL; the older Stable Diffusion 2.1 model is selected with --model sd. On a Mac with 8 GB of memory, quantization is practically necessary because the Stable Diffusion XL UNet alone has 2.6B parameters, according to the page. The image-to-image script resizes your input so its dimensions are divisible by 64 — resize it yourself first if you want full control. We did not run the example, so no image here is a recorded result.

Sources

See all