YiYiXu commited on
Commit
dcd8d9c
·
verified ·
1 Parent(s): 5653dca

Create block.py

Browse files
Files changed (1) hide show
  1. block.py +96 -0
block.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers.modular_pipelines import (
2
+ PipelineBlock,
3
+ InputParam,
4
+ OutputParam,
5
+ ConfigSpec,
6
+ )
7
+
8
+ from diffusers.utils import load_image
9
+ from PIL import Image
10
+ from typing import Union, Tuple
11
+
12
+ # copied from https://github.com/Wan-Video/Wan2.2/blob/388807310646ed5f318a99f8e8d9ad28c5b65373/wan/utils/utils.py#L136
13
+ def best_output_size(w, h, dw, dh, expected_area):
14
+ # float output size
15
+ ratio = w / h
16
+ ow = (expected_area * ratio)**0.5
17
+ oh = expected_area / ow
18
+
19
+ # process width first
20
+ ow1 = int(ow // dw * dw)
21
+ oh1 = int(expected_area / ow1 // dh * dh)
22
+ assert ow1 % dw == 0 and oh1 % dh == 0 and ow1 * oh1 <= expected_area
23
+ ratio1 = ow1 / oh1
24
+
25
+ # process height first
26
+ oh2 = int(oh // dh * dh)
27
+ ow2 = int(expected_area / oh2 // dw * dw)
28
+ assert oh2 % dh == 0 and ow2 % dw == 0 and ow2 * oh2 <= expected_area
29
+ ratio2 = ow2 / oh2
30
+
31
+ # compare ratios
32
+ if max(ratio / ratio1, ratio1 / ratio) < max(ratio / ratio2,
33
+ ratio2 / ratio):
34
+ return ow1, oh1
35
+ else:
36
+ return ow2, oh2
37
+
38
+ class Wan225BI2VImageProcessor(PipelineBlock):
39
+
40
+ @property
41
+ def description(self):
42
+ return "default Image Processor for wan2.2 5b i2v, it resizes the image to the best output size and center-crop it"
43
+
44
+ @property
45
+ def inputs(self):
46
+ return [
47
+ InputParam(name="image", type_hint=Union[Image.Image, str], description= "the Image to process"),
48
+ InputParam(name="max_area", type_hint=int, description= "the maximum area of the Image to process")
49
+ ]
50
+
51
+ @property
52
+ def intermediate_outputs(self):
53
+ return [
54
+ OutputParam(name="processed_image", type_hint=Image.Image, description= "the processed Image"),
55
+ ]
56
+
57
+ @property
58
+ def expected_configs(self):
59
+ return [
60
+ ConfigSpec(name="patch_size", default=(1, 2, 2)),
61
+ ConfigSpec(name="vae_stride", default=(4, 16, 16)),
62
+ ]
63
+
64
+ def __call__(self, components, state):
65
+
66
+ block_state = self.get_block_state(state)
67
+
68
+ if isinstance(block_state.image, str):
69
+ image = load_image(block_state.image).convert("RGB")
70
+ elif isinstance(block_state.image, Image.Image):
71
+ image = block_state.image
72
+ else:
73
+ raise ValueError(f"Invalid image type: {type(block_state.image)}; only support PIL Image or url string")
74
+
75
+ ih, iw = image.height, image.width
76
+ dh, dw = components.patch_size[1] * components.vae_stride[1], components.patch_size[2] * components.vae_stride[2]
77
+ ow, oh = best_output_size(iw, ih, dw, dh, block_state.max_area)
78
+
79
+ scale = max(ow / iw, oh / ih)
80
+ resized_image = image.resize((round(iw * scale), round(ih * scale)), Image.LANCZOS)
81
+
82
+ # center-crop
83
+ x1 = (resized_image.width - ow) // 2
84
+ y1 = (resized_image.height - oh) // 2
85
+ cropped_image = resized_image.crop((x1, y1, x1 + ow, y1 + oh))
86
+ assert cropped_image.width == ow and cropped_image.height == oh
87
+
88
+ block_state.processed_image = cropped_image
89
+
90
+ print(f" initial image size: {image.size}")
91
+ print(f" processed image size: {cropped_image.size}")
92
+
93
+
94
+ self.set_block_state(state, block_state)
95
+ return components, state
96
+