xilluill commited on
Commit
0f4d2e1
·
1 Parent(s): ae90f91

update resize

Browse files
Files changed (1) hide show
  1. app.py +40 -3
app.py CHANGED
@@ -31,6 +31,41 @@ class SamplingOptions:
31
  re_init: bool = False
32
  attn_mask: bool = False
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  @torch.inference_mode()
35
  def encode(init_image, torch_device):
36
  init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 127.5 - 1
@@ -68,13 +103,15 @@ def edit(brush_canvas,
68
 
69
  rgba_init_image = brush_canvas["background"]
70
  init_image = rgba_init_image[:,:,:3]
 
71
  shape = init_image.shape
72
  height = shape[0] if shape[0] % 16 == 0 else shape[0] - shape[0] % 16
73
  width = shape[1] if shape[1] % 16 == 0 else shape[1] - shape[1] % 16
74
  init_image = init_image[:height, :width, :]
75
  rgba_init_image = rgba_init_image[:height, :width, :]
76
 
77
- rgba_mask = brush_canvas["layers"][0][:height, :width, :]
 
78
  mask = rgba_mask[:,:,3]/255
79
  mask = mask.astype(int)
80
 
@@ -173,7 +210,7 @@ def create_demo(model_name: str):
173
  <b>Official 🤗 Gradio demo</b> for <a href='https://github.com/Xilluill/KV-Edit' target='_blank'><b>KV-Edit: Training-Free Image Editing for Precise Background Preservation</b></a>.<br>
174
 
175
  💫💫 <b>Here is editing steps:</b> (We highly recommend you run our code locally!😘 Only one inversion before multiple editing, very productive!) <br>
176
- 1️⃣ Upload your image that needs to be edited (The resolution must be less than 1360*768 because of memory.🙂) <br>
177
  2️⃣ Fill in your source prompt and use the brush tool to cover the area you want to edit (❗️required). <br>
178
  3️⃣ Fill in your target prompt, then adjust the hyperparameters. <br>
179
  4️⃣ Click the "Edit" button to generate your edited image! <br>
@@ -204,7 +241,7 @@ def create_demo(model_name: str):
204
  interactive=True,
205
  transforms=[],
206
  container=True,
207
- format='png',scale=1)
208
 
209
  edit_btn = gr.Button("edit")
210
 
 
31
  re_init: bool = False
32
  attn_mask: bool = False
33
 
34
+ def resize_image(image_array, max_width=1360, max_height=768):
35
+ # 将numpy数组转换为PIL图像
36
+ if image_array.shape[-1] == 4:
37
+ mode = 'RGBA'
38
+ else:
39
+ mode = 'RGB'
40
+
41
+ pil_image = Image.fromarray(image_array, mode=mode)
42
+
43
+ # 获取原始图像的宽度和高度
44
+ original_width, original_height = pil_image.size
45
+
46
+ # 计算缩放比例
47
+ width_ratio = max_width / original_width
48
+ height_ratio = max_height / original_height
49
+
50
+ # 选择较小的缩放比例以确保图像不超过最大宽度和高度
51
+ scale_ratio = min(width_ratio, height_ratio)
52
+
53
+ # 如果图像已经小于或等于最大分辨率,则不进行缩放
54
+ if scale_ratio >= 1:
55
+ return image_array
56
+
57
+ # 计算新的宽度和高度
58
+ new_width = int(original_width * scale_ratio)
59
+ new_height = int(original_height * scale_ratio)
60
+
61
+ # 缩放图像
62
+ resized_image = pil_image.resize((new_width, new_height))
63
+
64
+ # 将PIL图像转换回numpy数组
65
+ resized_array = np.array(resized_image)
66
+
67
+ return resized_array
68
+
69
  @torch.inference_mode()
70
  def encode(init_image, torch_device):
71
  init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 127.5 - 1
 
103
 
104
  rgba_init_image = brush_canvas["background"]
105
  init_image = rgba_init_image[:,:,:3]
106
+ init_image = resize_image(init_image)
107
  shape = init_image.shape
108
  height = shape[0] if shape[0] % 16 == 0 else shape[0] - shape[0] % 16
109
  width = shape[1] if shape[1] % 16 == 0 else shape[1] - shape[1] % 16
110
  init_image = init_image[:height, :width, :]
111
  rgba_init_image = rgba_init_image[:height, :width, :]
112
 
113
+ rgba_mask = brush_canvas["layers"][0]
114
+ rgba_mask = resize_image(rgba_mask)[:height, :width, :]
115
  mask = rgba_mask[:,:,3]/255
116
  mask = mask.astype(int)
117
 
 
210
  <b>Official 🤗 Gradio demo</b> for <a href='https://github.com/Xilluill/KV-Edit' target='_blank'><b>KV-Edit: Training-Free Image Editing for Precise Background Preservation</b></a>.<br>
211
 
212
  💫💫 <b>Here is editing steps:</b> (We highly recommend you run our code locally!😘 Only one inversion before multiple editing, very productive!) <br>
213
+ 1️⃣ Upload your image that needs to be edited (The resolution must be less than 1360*768 because of memory❗️) <br>
214
  2️⃣ Fill in your source prompt and use the brush tool to cover the area you want to edit (❗️required). <br>
215
  3️⃣ Fill in your target prompt, then adjust the hyperparameters. <br>
216
  4️⃣ Click the "Edit" button to generate your edited image! <br>
 
241
  interactive=True,
242
  transforms=[],
243
  container=True,
244
+ format='png')
245
 
246
  edit_btn = gr.Button("edit")
247