Spaces:
Running
Running
Commit
·
f8f22b5
1
Parent(s):
4af0d4b
f3
Browse files
utils.py
CHANGED
|
@@ -157,14 +157,7 @@ def generate_novelai_image(
|
|
| 157 |
payload['parameters']['extra_noise_seed'] = seed
|
| 158 |
payload["parameters"]['noise'] = i2i_noise
|
| 159 |
if chr_image != None:
|
| 160 |
-
|
| 161 |
-
if scale < 0.8165:
|
| 162 |
-
chr_image = chr_image.resize((1024, 1536))
|
| 163 |
-
elif scale < 1.2247:
|
| 164 |
-
chr_image = chr_image.resize((1427, 1427))
|
| 165 |
-
else:
|
| 166 |
-
chr_image = chr_image.resize((1536, 1024))
|
| 167 |
-
payload['parameters']['director_reference_images'] = [image2base64(chr_image)]
|
| 168 |
payload['parameters']['director_reference_descriptions'] = [{'caption': {'base_caption': 'character&style' if style_aware else 'character', 'char_captions': []}, 'legacy_uc': False}]
|
| 169 |
payload['parameters']['director_reference_secondary_strength_values'] = [1 - fidelity]
|
| 170 |
payload['parameters']['director_reference_strength_values'] = [1]
|
|
@@ -208,6 +201,65 @@ def base642image(b64):
|
|
| 208 |
img = Image.open(read_buffer)
|
| 209 |
return img
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
def augment_image(image, width, height, req_type, selection, factor=1, defry=0, prompt=''):
|
| 212 |
if selection == "scale":
|
| 213 |
width = int(width * factor)
|
|
|
|
| 157 |
payload['parameters']['extra_noise_seed'] = seed
|
| 158 |
payload["parameters"]['noise'] = i2i_noise
|
| 159 |
if chr_image != None:
|
| 160 |
+
payload['parameters']['director_reference_images'] = [image2base64(resize_and_pad_image(chr_image))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
payload['parameters']['director_reference_descriptions'] = [{'caption': {'base_caption': 'character&style' if style_aware else 'character', 'char_captions': []}, 'legacy_uc': False}]
|
| 162 |
payload['parameters']['director_reference_secondary_strength_values'] = [1 - fidelity]
|
| 163 |
payload['parameters']['director_reference_strength_values'] = [1]
|
|
|
|
| 201 |
img = Image.open(read_buffer)
|
| 202 |
return img
|
| 203 |
|
| 204 |
+
def resize_and_pad_image(chr_image):
|
| 205 |
+
"""
|
| 206 |
+
根据图像宽高比选择最接近的尺寸,等比缩放并用黑边填充
|
| 207 |
+
|
| 208 |
+
Args:
|
| 209 |
+
chr_image: PIL Image对象
|
| 210 |
+
|
| 211 |
+
Returns:
|
| 212 |
+
处理后的PIL Image对象
|
| 213 |
+
"""
|
| 214 |
+
# 定义三种目标尺寸
|
| 215 |
+
target_sizes = [
|
| 216 |
+
(1024, 1536), # 竖版
|
| 217 |
+
(1472, 1472), # 正方形
|
| 218 |
+
(1536, 1024) # 横版
|
| 219 |
+
]
|
| 220 |
+
|
| 221 |
+
# 获取原图尺寸
|
| 222 |
+
original_width, original_height = chr_image.size
|
| 223 |
+
original_ratio = original_width / original_height
|
| 224 |
+
|
| 225 |
+
# 计算每个目标尺寸的宽高比,并找出最接近的
|
| 226 |
+
min_diff = float('inf')
|
| 227 |
+
best_size = None
|
| 228 |
+
|
| 229 |
+
for size in target_sizes:
|
| 230 |
+
target_ratio = size[0] / size[1]
|
| 231 |
+
diff = abs(original_ratio - target_ratio)
|
| 232 |
+
if diff < min_diff:
|
| 233 |
+
min_diff = diff
|
| 234 |
+
best_size = size
|
| 235 |
+
|
| 236 |
+
# 目标尺寸
|
| 237 |
+
target_width, target_height = best_size
|
| 238 |
+
|
| 239 |
+
# 计算缩放比例(保持宽高比)
|
| 240 |
+
scale_x = target_width / original_width
|
| 241 |
+
scale_y = target_height / original_height
|
| 242 |
+
scale = min(scale_x, scale_y) # 选择较小的缩放比例以确保图像完全显示
|
| 243 |
+
|
| 244 |
+
# 计算缩放后的尺寸
|
| 245 |
+
new_width = int(original_width * scale)
|
| 246 |
+
new_height = int(original_height * scale)
|
| 247 |
+
|
| 248 |
+
# 缩放图像
|
| 249 |
+
resized_image = chr_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 250 |
+
|
| 251 |
+
# 创建目标尺寸的黑色背景
|
| 252 |
+
padded_image = Image.new('RGBA', (target_width, target_height), color='black')
|
| 253 |
+
|
| 254 |
+
# 计算居中位置
|
| 255 |
+
x_offset = (target_width - new_width) // 2
|
| 256 |
+
y_offset = (target_height - new_height) // 2
|
| 257 |
+
|
| 258 |
+
# 将缩放后的图像粘贴到黑色背景上
|
| 259 |
+
padded_image.paste(resized_image, (x_offset, y_offset))
|
| 260 |
+
|
| 261 |
+
return padded_image
|
| 262 |
+
|
| 263 |
def augment_image(image, width, height, req_type, selection, factor=1, defry=0, prompt=''):
|
| 264 |
if selection == "scale":
|
| 265 |
width = int(width * factor)
|