11from diffsynth import ModelManager , FluxImagePipeline , download_customized_models
2- from diffsynth .data .video import crop_and_resize
32from modelscope import dataset_snapshot_download
43from examples .EntityControl .utils import visualize_masks
54from PIL import Image
6- import numpy as np
75import torch
86
97
10-
118def build_pipeline ():
129 model_manager = ModelManager (torch_dtype = torch .bfloat16 , device = "cuda" , model_id_list = ["FLUX.1-dev" ])
1310 model_manager .load_lora (
@@ -30,16 +27,13 @@ def build_pipeline():
3027 return pipe
3128
3229
33- def generate (pipe : FluxImagePipeline , logo_image , target_image , mask , height , width , prompt , logo_prompt , image_save_path , mask_save_path ):
34- mask = Image .fromarray (np .concatenate ([
35- np .ones ((height , width , 3 ), dtype = np .uint8 ) * 0 ,
36- np .array (crop_and_resize (mask , height , width )),
37- ], axis = 1 ))
30+ def generate (pipe : FluxImagePipeline , source_image , target_image , mask , height , width , prompt , entity_prompt , image_save_path , mask_save_path , seed = 0 ):
31+ input_mask = Image .new ('RGB' , (width * 2 , height ))
32+ input_mask .paste (mask .resize ((width , height ), resample = Image .NEAREST ).convert ('RGB' ), (width , 0 ))
3833
39- input_image = Image .fromarray (np .concatenate ([
40- np .array (crop_and_resize (logo_image , height , width )),
41- np .array (crop_and_resize (target_image , height , width )),
42- ], axis = 1 ))
34+ input_image = Image .new ('RGB' , (width * 2 , height ))
35+ input_image .paste (source_image .resize ((width , height )).convert ('RGB' ), (0 , 0 ))
36+ input_image .paste (target_image .resize ((width , height )).convert ('RGB' ), (width , 0 ))
4337
4438 image = pipe (
4539 prompt = prompt ,
@@ -48,41 +42,43 @@ def generate(pipe: FluxImagePipeline, logo_image, target_image, mask, height, wi
4842 negative_prompt = "" ,
4943 num_inference_steps = 50 ,
5044 embedded_guidance = 3.5 ,
51- seed = 0 ,
45+ seed = seed ,
5246 height = height ,
5347 width = width * 2 ,
54- eligen_entity_prompts = [logo_prompt ],
55- eligen_entity_masks = [mask ],
48+ eligen_entity_prompts = [entity_prompt ],
49+ eligen_entity_masks = [input_mask ],
5650 enable_eligen_on_negative = False ,
5751 enable_eligen_inpaint = True ,
5852 )
59- image .save (image_save_path )
60- visualize_masks (image , [mask ], [logo_prompt ], mask_save_path )
53+ target_image = image .crop ((width , 0 , 2 * width , height ))
54+ target_image .save (image_save_path )
55+ visualize_masks (target_image , [mask ], [entity_prompt ], mask_save_path )
56+ return target_image
6157
6258
6359pipe = build_pipeline ()
6460
6561dataset_snapshot_download (dataset_id = "DiffSynth-Studio/examples_in_diffsynth" , local_dir = "./" , allow_file_pattern = "data/examples/eligen/logo_transfer/*" )
66- logo_image = Image .open ("data/examples/eligen/logo_transfer/logo_transfer_logo.png" )
67- target_image = Image .open ("data/examples/eligen/logo_transfer/logo_transfer_target_image.png" )
6862
6963prompt = "The two-panel image showcases the joyful identity, with the left panel showing a rabbit graphic; [LEFT] while the right panel translates the design onto a shopping tote with the rabbit logo in black, held by a person in a market setting, emphasizing the brand's approachable and eco-friendly vibe."
7064logo_prompt = "a rabbit logo"
7165
72- mask = Image .open ("data/examples/eligen/logo_transfer/logo_transfer_mask_1.png" )
66+ logo_image = Image .open ("data/examples/eligen/logo_transfer/source_image.png" )
67+ target_image = Image .open ("data/examples/eligen/logo_transfer/target_image.png" )
68+ mask = Image .open ("data/examples/eligen/logo_transfer/mask_1.png" )
7369generate (
7470 pipe , logo_image , target_image , mask ,
75- height = 1024 , width = 736 ,
76- prompt = prompt , logo_prompt = logo_prompt ,
71+ height = 1024 , width = 1024 ,
72+ prompt = prompt , entity_prompt = logo_prompt ,
7773 image_save_path = "entity_transfer_1.png" ,
7874 mask_save_path = "entity_transfer_with_mask_1.png"
7975)
8076
81- mask = Image .open ("data/examples/eligen/logo_transfer/logo_transfer_mask_2 .png" )
77+ mask = Image .open ("data/examples/eligen/logo_transfer/mask_2 .png" )
8278generate (
8379 pipe , logo_image , target_image , mask ,
84- height = 1024 , width = 736 ,
85- prompt = prompt , logo_prompt = logo_prompt ,
80+ height = 1024 , width = 1024 ,
81+ prompt = prompt , entity_prompt = logo_prompt ,
8682 image_save_path = "entity_transfer_2.png" ,
8783 mask_save_path = "entity_transfer_with_mask_2.png"
8884)
0 commit comments