Update flux1_img2img.py
Browse files- flux1_img2img.py +36 -36
flux1_img2img.py
CHANGED
@@ -1,36 +1,36 @@
|
|
1 |
-
import torch
|
2 |
-
from diffusers import FluxImg2ImgPipeline
|
3 |
-
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
print(prompt)
|
25 |
-
output = pipe(prompt=prompt, image=image,generator=generator,strength=strength
|
26 |
-
|
27 |
-
|
28 |
-
# TODO support mask
|
29 |
-
return output.images[0]
|
30 |
-
|
31 |
-
if __name__ == "__main__":
|
32 |
-
#args input-image input-mask output
|
33 |
-
image = Image.open(sys.argv[1])
|
34 |
-
mask = Image.open(sys.argv[2])
|
35 |
-
output = process_image(image,mask)
|
36 |
-
output.save(sys.argv[3])
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import FluxImg2ImgPipeline
|
3 |
+
from PIL import Image
|
4 |
+
import sys
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# I only test with FLUX.1-schnell
|
8 |
+
|
9 |
+
@spaces.GPU
|
10 |
+
def process_image(image, mask_image, prompt="a person", model_id="black-forest-labs/FLUX.1-schnell", strength=0.75, seed=0, num_inference_steps=4):
|
11 |
+
print("start process image process_image")
|
12 |
+
if image is None:
|
13 |
+
print("empty input image returned")
|
14 |
+
return None
|
15 |
+
|
16 |
+
# Ensure image is in RGB mode (helps with WebP and other formats)
|
17 |
+
if image.mode != "RGB":
|
18 |
+
image = image.convert("RGB")
|
19 |
+
|
20 |
+
pipe = FluxImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
21 |
+
pipe.to("cuda")
|
22 |
+
|
23 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
24 |
+
print(prompt)
|
25 |
+
output = pipe(prompt=prompt, image=image, generator=generator, strength=strength,
|
26 |
+
guidance_scale=0, num_inference_steps=num_inference_steps, max_sequence_length=256)
|
27 |
+
|
28 |
+
# TODO: support mask
|
29 |
+
return output.images[0]
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
# args: input-image input-mask output
|
33 |
+
image = Image.open(sys.argv[1])
|
34 |
+
mask = Image.open(sys.argv[2])
|
35 |
+
output = process_image(image, mask)
|
36 |
+
output.save(sys.argv[3])
|