Views
No views yet
1image_processor = ModularPipeline.from_pretrained("YiYiXu/WanImageProcessor", trust_remote_code=True)
2image = image_processor(
3 image="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/wan_i2v_input.JPG",
4 max_area=1280*704, output="processed_image")1# copied from https://github.com/Wan-Video/Wan2.2/blob/388807310646ed5f318a99f8e8d9ad28c5b65373/wan/utils/utils.py#L136
2def best_output_size(w, h, dw, dh, expected_area):
3 # float output size
4 ratio = w / h
5 ow = (expected_area * ratio)**0.5
6 oh = expected_area / ow
7
8 # process width first
9 ow1 = int(ow // dw * dw)
10 oh1 = int(expected_area / ow1 // dh * dh)
11 assert ow1 % dw == 0 and oh1 % dh == 0 and ow1 * oh1 <= expected_area
12 ratio1 = ow1 / oh1
13
14 # process height first
15 oh2 = int(oh // dh * dh)
16 ow2 = int(expected_area / oh2 // dw * dw)
17 assert oh2 % dh == 0 and ow2 % dw == 0 and ow2 * oh2 <= expected_area
18 ratio2 = ow2 / oh2
19
20 # compare ratios
21 if max(ratio / ratio1, ratio1 / ratio) < max(ratio / ratio2,
22 ratio2 / ratio):
23 return ow1, oh1
24 else:
25 return ow2, oh2
26
27from diffusers.utils import load_image
28
29image = load_image(block_state.image).convert("RGB")
30max_area = 1280*704
31
32ih, iw = image.height, image.width
33dh, dw = pipe.transformer.config.patch_size[1] * pipe.vae_scale_factor_spatial, pipe.transformer.config.patch_size[2] * pipe.vae_scale_factor_spatial
34ow, oh = best_output_size(iw, ih, dw, dh, max_area)
35
36scale = max(ow / iw, oh / ih)
37resized_image = image.resize((round(iw * scale), round(ih * scale)), Image.LANCZOS)
38
39# center-crop
40x1 = (resized_image.width - ow) // 2
41y1 = (resized_image.height - oh) // 2
42image = resized_image.crop((x1, y1, x1 + ow, y1 + oh))
43