Views
No views yet

1import torch
2from controlnet_flux import FluxControlNetModel
3from pipeline_flux_controlnet import FluxControlNetPipeline
4
5from PIL import Image, ImageDraw, ImageFont
6import numpy as np
7import cv2
8import re
9import os
10
11def contains_chinese(text):
12 if re.search(r'[\u4e00-\u9fff]', text):
13 return True
14 return False
15
16def canny(img):
17 low_threshold = 50
18 high_threshold = 100
19 img = cv2.Canny(img, low_threshold, high_threshold)
20 img = img[:, :, None]
21 img = 255 - np.concatenate([img, img, img], axis=2)
22 return img
23
24base_model = "black-forest-labs/FLUX.1-dev"
25controlnet_model = "Shakker-Labs/RepText"
26
27controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16)
28pipe = FluxControlNetPipeline.from_pretrained(
29 base_model, controlnet=controlnet, torch_dtype=torch.bfloat16
30).to("cuda")
31
32## set resolution
33width, height = 1024, 1024
34
35## set font
36font_path = "./assets/Arial_Unicode.ttf" # use your own font
37font_size = 80 # it is recommended to use a font size >= 60
38font = ImageFont.truetype(font_path, font_size)
39
40## set text content, position, color
41text_list = ["哩布哩布"]
42text_position_list = [(370, 200)]
43text_color_list = [(255, 255, 255)]
44
45## set controlnet conditions
46control_image_list = [] # canny list
47control_position_list = [] # position list
48control_mask_list = [] # regional mask list
49control_glyph_all = np.zeros([height, width, 3], dtype=np.uint8) # all glyphs
50
51## handle each line of text
52for text, text_position, text_color in zip(text_list, text_position_list, text_color_list):
53
54 ### glyph image, render text to black background
55 control_image_glyph = Image.new("RGB", (width, height), (0, 0, 0))
56 draw = ImageDraw.Draw(control_image_glyph)
57 draw.text(text_position, text, font=font, fill=text_color)
58
59 ### get bbox
60 bbox = draw.textbbox(text_position, text, font=font)
61
62 ### position condition
63 control_position = np.zeros([height, width], dtype=np.uint8)
64 control_position[bbox[1]:bbox[3], bbox[0]:bbox[2]] = 255
65 control_position = Image.fromarray(control_position.astype(np.uint8))
66 control_position_list.append(control_position)
67
68 ### regional mask
69 control_mask_np = np.zeros([height, width], dtype=np.uint8)
70 control_mask_np[bbox[1]-5:bbox[3]+5, bbox[0]-5:bbox[2]+5] = 255
71 control_mask = Image.fromarray(control_mask_np.astype(np.uint8))
72 control_mask_list.append(control_mask)
73
74 ### accumulate glyph
75 control_glyph = np.array(control_image_glyph)
76 control_glyph_all += control_glyph
77
78 ### canny condition
79 control_image = canny(cv2.cvtColor(np.array(control_image_glyph), cv2.COLOR_RGB2BGR))
80 control_image = Image.fromarray(cv2.cvtColor(control_image, cv2.COLOR_BGR2RGB))
81 control_image_list.append(control_image)
82
83control_glyph_all = Image.fromarray(control_glyph_all.astype(np.uint8))
84control_glyph_all = control_glyph_all.convert("RGB")
85# control_glyph_all.save("./results/control_glyph.jpg")
86
87# it is recommended to use words such 'sign', 'billboard', 'banner' in your prompt
88# for Englith text, it helps if you add the text to the prompt
89prompt = "a street sign in city"
90for text in text_list:
91 if not contains_chinese(text):
92 prompt += f", '{text}'"
93prompt += ", filmfotos, film grain, reversal film photography" # optional
94print(prompt)
95
96generator = torch.Generator(device="cuda").manual_seed(42)
97
98image = pipe(
99 prompt,
100 control_image=control_image_list, # canny
101 control_position=control_position_list, # position
102 control_mask=control_mask_list, # regional mask
103 control_glyph=control_glyph_all, # as init latent, optional, set to None if not used
104 controlnet_conditioning_scale=1.0,
105 controlnet_conditioning_step=30,
106 width=width,
107 height=height,
108 num_inference_steps=30,
109 guidance_scale=3.5,
110 generator=generator,
111).images[0]
112
113if not os.path.exists("./results"):
114 os.makedirs("./results")
115image.save(f"./results/result.jpg")1@article{wang2025reptext,
2 title={RepText: Rendering Visual Text via Replicating},
3 author={Wang, Haofan and Xu, Yujia and Li, Yimeng and Li, Junchen and Zhang, Chaowei and Wang, Jing and Yang, Kejia and Chen, Zhibo},
4 journal={arXiv preprint arXiv:2504.19724},
5 year={2025}
6}haofanwang.ai@gmail.com.