Views
No views yet
FLUX.1 Fill [dev] is a 12 billion parameter rectified flow transformer capable of filling areas in existing images based on a text description.


FLUX.1 Fill [dev] with the 🧨 diffusers python library, first install or upgrade diffuserspip install -U diffusersFluxFillPipeline to run the model
Here is a code snippet to use the code.1import numpy as np
2import cv2
3from PIL import Image
4import torch
5from diffusers import FluxFillPipeline
6from diffusers.utils import load_image
7from typing import Union
8
9def prepare_masked_image(
10 foreground: Union[Image.Image, np.ndarray],
11 mask: Union[Image.Image, np.ndarray],
12 alpha: float = 0.001,
13 blur: bool = True
14) -> Image.Image:
15 """
16 Combines the foreground and mask to produce a masked image with noise in the masked region.
17
18 Args:
19 foreground (PIL.Image.Image or np.ndarray): The input image to be inpainted.
20 mask (PIL.Image.Image or np.ndarray): A binary mask (0 or 255) indicating the foreground region.
21 alpha (float): Blending factor for noise. Lower alpha → more noise in the masked area.
22 blur (bool): Whether to blur the randomly generated noise.
23
24 Returns:
25 PIL.Image.Image: The resulting masked image with noise in the masked area.
26 """
27
28 # Ensure foreground is an ndarray
29 if isinstance(foreground, Image.Image):
30 foreground_np = np.array(foreground)
31 else:
32 foreground_np = foreground # assume already a NumPy array
33
34 # Ensure mask is a NumPy array and single-channel
35 if isinstance(mask, Image.Image):
36 mask_np = np.array(mask.convert("L")) # convert to grayscale
37 else:
38 mask_np = mask
39 if mask_np.ndim == 3:
40 mask_np = cv2.cvtColor(mask_np, cv2.COLOR_BGR2GRAY)
41
42 h, w, c = foreground_np.shape # height, width, channels
43
44 # Create 3×3 kernel for dilation (used later)
45 kernel = np.ones((3, 3), np.uint8)
46
47 # Generate random Gaussian noise
48 noise = np.random.rand(h, w) * 255
49 noise = noise.astype(np.uint8)
50 if blur:
51 noise = cv2.GaussianBlur(noise, (5, 5), 0)
52 # Stack to 3 channels
53 noise_rgb = np.stack([noise, noise, noise], axis=-1)
54
55 # Prepare a black background
56 black_bg = np.zeros_like(foreground_np, dtype=np.uint8)
57
58 # Dilate the mask to get smoother boundaries for seamlessClone
59 dilated_mask = cv2.dilate(mask_np, kernel, iterations=10)
60
61 # Compute center for seamlessClone (center of the image)
62 center = (w // 2, h // 2)
63
64 # Use mixed clone to merge the foreground onto a black background, using the dilated mask
65 cloned = cv2.seamlessClone(
66 src=foreground_np,
67 dst=black_bg,
68 mask=dilated_mask,
69 p=center,
70 flags=cv2.MIXED_CLONE
71 )
72
73 # Blend cloned result (mostly black except where mask is) with noise
74 noisy_bg = (alpha * cloned + (1 - alpha) * noise_rgb).astype(np.uint8)
75
76 # Normalize mask to [0,1] float if it’s in [0,255]
77
78 if mask_np.max() <= 1:
79 mask_norm = mask_np.astype(np.float32)
80 else:
81 mask_norm = (mask_np / 255.0).astype(np.float32)
82
83 # Expand mask to 3 channels if needed
84 if mask_norm.ndim == 2:
85 mask_norm = np.stack([mask_norm] * 3, axis=-1)
86
87 # Combine: keep original pixels where mask=0, use noisy_bg where mask=1
88 combined = ((1 - mask_norm) * noisy_bg + mask_norm * foreground_np).astype(np.uint8)
89
90 return Image.fromarray(combined)
91
92
93def main():
94 """Entry point for running the FluxFill pipeline."""
95 # Load input image and its corresponding mask
96 fg_mask = load_image("https://huggingface.co/rkv1990/FLUX.1-Fill-dev-outpainting/resolve/main/beauty-products-mask.png").convert("L")
97 input_image= load_image("https://huggingface.co/rkv1990/FLUX.1-Fill-dev-outpainting/resolve/main/beauty-products.png").convert("RGB")
98 inpaint_mask = np.array(255-np.array(fg_mask))
99 w,h = input_image.size
100 masked_image = prepare_masked_image(foreground=input_image, mask=fg_mask)
101
102 # Initialize the FluxFill pipeline
103 pipe = FluxFillPipeline.from_pretrained(
104 "black-forest-labs/FLUX.1-Fill-dev",
105 torch_dtype=torch.bfloat16
106 ).to("cuda")
107
108 # Run the pipeline
109 output = pipe(
110 prompt="A mist-covered forest at dawn, with pale golden light filtering through ancient, twisted trees. Soft fog swirls around delicate wildflowers glowing faintly with bioluminescence.",
111 image=masked_image,
112 mask_image=inpaint_mask,
113 height=h,
114 width=w,
115 guidance_scale=30,
116 num_inference_steps=50,
117 max_sequence_length=512,
118 generator=torch.Generator(device="cpu").manual_seed(0)
119 ).images[0]
120
121 # Save the resulting image
122 output.save("flux-fill-dev.png")
123 print("Saved output to flux-fill-dev.png")
124
125
126if __name__ == "__main__":
127 main()
128