|
3 | 3 |
|
4 | 4 | import folder_paths |
5 | 5 | import torch |
| 6 | +from PIL import Image |
6 | 7 |
|
7 | 8 | from bizyair import BizyAirBaseNode, BizyAirNodeIO, create_node_data |
8 | 9 | from bizyair.data_types import CLIP, CONDITIONING, MODEL |
9 | 10 |
|
| 11 | +from .utils import T, contrast_adaptive_sharpening |
| 12 | + |
10 | 13 | # set the models directory |
11 | 14 | if "ipadapter" not in folder_paths.folder_names_and_paths: |
12 | 15 | current_paths = [os.path.join(folder_paths.models_dir, "ipadapter")] |
@@ -1207,76 +1210,76 @@ def INPUT_TYPES(s): |
1207 | 1210 | # return (noise,) |
1208 | 1211 |
|
1209 | 1212 |
|
1210 | | -# class PrepImageForClipVision: |
1211 | | -# @classmethod |
1212 | | -# def INPUT_TYPES(s): |
1213 | | -# return { |
1214 | | -# "required": { |
1215 | | -# "image": ("IMAGE",), |
1216 | | -# "interpolation": ( |
1217 | | -# ["LANCZOS", "BICUBIC", "HAMMING", "BILINEAR", "BOX", "NEAREST"], |
1218 | | -# ), |
1219 | | -# "crop_position": (["top", "bottom", "left", "right", "center", "pad"],), |
1220 | | -# "sharpening": ( |
1221 | | -# "FLOAT", |
1222 | | -# {"default": 0.0, "min": 0, "max": 1, "step": 0.05}, |
1223 | | -# ), |
1224 | | -# }, |
1225 | | -# } |
1226 | | - |
1227 | | -# RETURN_TYPES = ("IMAGE",) |
1228 | | -# FUNCTION = "prep_image" |
1229 | | - |
1230 | | -# CATEGORY = "ipadapter/utils" |
1231 | | - |
1232 | | -# def prep_image( |
1233 | | -# self, image, interpolation="LANCZOS", crop_position="center", sharpening=0.0 |
1234 | | -# ): |
1235 | | -# size = (224, 224) |
1236 | | -# _, oh, ow, _ = image.shape |
1237 | | -# output = image.permute([0, 3, 1, 2]) |
1238 | | - |
1239 | | -# if crop_position == "pad": |
1240 | | -# if oh != ow: |
1241 | | -# if oh > ow: |
1242 | | -# pad = (oh - ow) // 2 |
1243 | | -# pad = (pad, 0, pad, 0) |
1244 | | -# elif ow > oh: |
1245 | | -# pad = (ow - oh) // 2 |
1246 | | -# pad = (0, pad, 0, pad) |
1247 | | -# output = T.functional.pad(output, pad, fill=0) |
1248 | | -# else: |
1249 | | -# crop_size = min(oh, ow) |
1250 | | -# x = (ow - crop_size) // 2 |
1251 | | -# y = (oh - crop_size) // 2 |
1252 | | -# if "top" in crop_position: |
1253 | | -# y = 0 |
1254 | | -# elif "bottom" in crop_position: |
1255 | | -# y = oh - crop_size |
1256 | | -# elif "left" in crop_position: |
1257 | | -# x = 0 |
1258 | | -# elif "right" in crop_position: |
1259 | | -# x = ow - crop_size |
1260 | | - |
1261 | | -# x2 = x + crop_size |
1262 | | -# y2 = y + crop_size |
1263 | | - |
1264 | | -# output = output[:, :, y:y2, x:x2] |
1265 | | - |
1266 | | -# imgs = [] |
1267 | | -# for img in output: |
1268 | | -# img = T.ToPILImage()(img) # using PIL for better results |
1269 | | -# img = img.resize(size, resample=Image.Resampling[interpolation]) |
1270 | | -# imgs.append(T.ToTensor()(img)) |
1271 | | -# output = torch.stack(imgs, dim=0) |
1272 | | -# del imgs, img |
1273 | | - |
1274 | | -# if sharpening > 0: |
1275 | | -# output = contrast_adaptive_sharpening(output, sharpening) |
1276 | | - |
1277 | | -# output = output.permute([0, 2, 3, 1]) |
| 1213 | +class PrepImageForClipVision(BizyAirBaseNode): |
| 1214 | + @classmethod |
| 1215 | + def INPUT_TYPES(s): |
| 1216 | + return { |
| 1217 | + "required": { |
| 1218 | + "image": ("IMAGE",), |
| 1219 | + "interpolation": ( |
| 1220 | + ["LANCZOS", "BICUBIC", "HAMMING", "BILINEAR", "BOX", "NEAREST"], |
| 1221 | + ), |
| 1222 | + "crop_position": (["top", "bottom", "left", "right", "center", "pad"],), |
| 1223 | + "sharpening": ( |
| 1224 | + "FLOAT", |
| 1225 | + {"default": 0.0, "min": 0, "max": 1, "step": 0.05}, |
| 1226 | + ), |
| 1227 | + }, |
| 1228 | + } |
1278 | 1229 |
|
1279 | | -# return (output,) |
| 1230 | + RETURN_TYPES = ("IMAGE",) |
| 1231 | + FUNCTION = "prep_image" |
| 1232 | + NODE_DISPLAY_NAME = "Prep Image For ClipVision" |
| 1233 | + CATEGORY = "ipadapter/utils" |
| 1234 | + |
| 1235 | + def prep_image( |
| 1236 | + self, image, interpolation="LANCZOS", crop_position="center", sharpening=0.0 |
| 1237 | + ): |
| 1238 | + size = (224, 224) |
| 1239 | + _, oh, ow, _ = image.shape |
| 1240 | + output = image.permute([0, 3, 1, 2]) |
| 1241 | + |
| 1242 | + if crop_position == "pad": |
| 1243 | + if oh != ow: |
| 1244 | + if oh > ow: |
| 1245 | + pad = (oh - ow) // 2 |
| 1246 | + pad = (pad, 0, pad, 0) |
| 1247 | + elif ow > oh: |
| 1248 | + pad = (ow - oh) // 2 |
| 1249 | + pad = (0, pad, 0, pad) |
| 1250 | + output = T.functional.pad(output, pad, fill=0) |
| 1251 | + else: |
| 1252 | + crop_size = min(oh, ow) |
| 1253 | + x = (ow - crop_size) // 2 |
| 1254 | + y = (oh - crop_size) // 2 |
| 1255 | + if "top" in crop_position: |
| 1256 | + y = 0 |
| 1257 | + elif "bottom" in crop_position: |
| 1258 | + y = oh - crop_size |
| 1259 | + elif "left" in crop_position: |
| 1260 | + x = 0 |
| 1261 | + elif "right" in crop_position: |
| 1262 | + x = ow - crop_size |
| 1263 | + |
| 1264 | + x2 = x + crop_size |
| 1265 | + y2 = y + crop_size |
| 1266 | + |
| 1267 | + output = output[:, :, y:y2, x:x2] |
| 1268 | + |
| 1269 | + imgs = [] |
| 1270 | + for img in output: |
| 1271 | + img = T.ToPILImage()(img) # using PIL for better results |
| 1272 | + img = img.resize(size, resample=Image.Resampling[interpolation]) |
| 1273 | + imgs.append(T.ToTensor()(img)) |
| 1274 | + output = torch.stack(imgs, dim=0) |
| 1275 | + del imgs, img |
| 1276 | + |
| 1277 | + if sharpening > 0: |
| 1278 | + output = contrast_adaptive_sharpening(output, sharpening) |
| 1279 | + |
| 1280 | + output = output.permute([0, 2, 3, 1]) |
| 1281 | + |
| 1282 | + return (output,) |
1280 | 1283 |
|
1281 | 1284 |
|
1282 | 1285 | # class IPAdapterSaveEmbeds: |
|
0 commit comments