wren93 commited on
Commit
bc41fc6
·
1 Parent(s): 9eee539
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -52,15 +52,28 @@ class AnimateController:
52
  img_path = os.path.join(self.savedir, "input_image.png")
53
  pil_image.save(img_path)
54
  self.image_resolution = pil_image.size
55
- pil_image = pil_image.resize((width_slider, height_slider))
 
56
  if center_crop:
57
- width, height = width_slider, height_slider
58
- aspect_ratio = width / height
59
- if aspect_ratio > 16 / 10:
60
- pil_image = pil_image.crop((int((width - height * 16 / 10) / 2), 0, int((width + height * 16 / 10) / 2), height))
61
- elif aspect_ratio < 16 / 10:
62
- pil_image = pil_image.crop((0, int((height - width * 10 / 16) / 2), width, int((height + width * 10 / 16) / 2)))
63
- return gr.Textbox.change(value=img_path), gr.Image.change(value=np.array(pil_image))
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  @spaces.GPU
66
  def animate(
@@ -180,7 +193,7 @@ class AnimateController:
180
  f.write(json_str)
181
  f.write("\n\n")
182
 
183
- return gr.Video.change(value=save_sample_path)
184
 
185
 
186
  controller = AnimateController()
@@ -198,7 +211,7 @@ def ui():
198
  with gr.Column(variant="panel"):
199
  gr.Markdown(
200
  """
201
- - Input image can be specified using the "Input Image Path/URL" text box (this can be either a local image path or an image URL) or uploaded by clicking or dragging the image to the "Input Image" box. The uploaded image will be temporarily stored in the "samples/Gradio" folder under the project root folder.
202
  - Input image can be resized and/or center cropped to a given resolution by adjusting the "Width" and "Height" sliders. It is recommended to use the same resolution as the training resolution (256x256).
203
  - After setting the input image path or changed the width/height of the input image, press the "Preview" button to visualize the resized input image.
204
  """
@@ -230,7 +243,7 @@ def ui():
230
 
231
  seed_textbox = gr.Textbox(label="Seed", value=-1)
232
  seed_button = gr.Button(value="\U0001F3B2", elem_classes="toolbutton")
233
- seed_button.click(fn=lambda: gr.Textbox.change(value=random.randint(1, 1e8)), inputs=[], outputs=[seed_textbox])
234
 
235
 
236
 
@@ -238,7 +251,7 @@ def ui():
238
 
239
  with gr.Column():
240
  with gr.Row():
241
- input_image_path = gr.Textbox(label="Input Image Path/URL", lines=1, scale=10, info="Press Enter or the Preview button to confirm the input image.")
242
  preview_button = gr.Button(value="Preview")
243
 
244
  with gr.Row():
@@ -273,7 +286,7 @@ def ui():
273
  pil_image = pil_image.crop((left, top, right, bottom))
274
 
275
  pil_image = pil_image.resize((width_slider, height_slider))
276
- return gr.Image.change(value=np.array(pil_image))
277
 
278
  preview_button.click(fn=update_and_resize_image, inputs=[input_image_path, height_slider, width_slider, center_crop], outputs=[input_image])
279
  input_image_path.submit(fn=update_and_resize_image, inputs=[input_image_path, height_slider, width_slider, center_crop], outputs=[input_image])
 
52
  img_path = os.path.join(self.savedir, "input_image.png")
53
  pil_image.save(img_path)
54
  self.image_resolution = pil_image.size
55
+
56
+ original_width, original_height = pil_image.size
57
  if center_crop:
58
+ crop_aspect_ratio = width_slider / height_slider
59
+ aspect_ratio = original_width / original_height
60
+ if aspect_ratio > crop_aspect_ratio:
61
+ new_width = int(crop_aspect_ratio * original_height)
62
+ left = (original_width - new_width) / 2
63
+ top = 0
64
+ right = left + new_width
65
+ bottom = original_height
66
+ pil_image = pil_image.crop((left, top, right, bottom))
67
+ elif aspect_ratio < crop_aspect_ratio:
68
+ new_height = int(original_width / crop_aspect_ratio)
69
+ top = (original_height - new_height) / 2
70
+ left = 0
71
+ right = original_width
72
+ bottom = top + new_height
73
+ pil_image = pil_image.crop((left, top, right, bottom))
74
+
75
+ pil_image = pil_image.resize((width_slider, height_slider))
76
+ return gr.Textbox(value=img_path), gr.Image(value=np.array(pil_image))
77
 
78
  @spaces.GPU
79
  def animate(
 
193
  f.write(json_str)
194
  f.write("\n\n")
195
 
196
+ return gr.Video(value=save_sample_path)
197
 
198
 
199
  controller = AnimateController()
 
211
  with gr.Column(variant="panel"):
212
  gr.Markdown(
213
  """
214
+ - Input image can be specified using the "Input Image URL" text box or uploaded by clicking or dragging the image to the "Input Image" box. The uploaded image will be temporarily stored in the "samples/Gradio" folder under the project root folder.
215
  - Input image can be resized and/or center cropped to a given resolution by adjusting the "Width" and "Height" sliders. It is recommended to use the same resolution as the training resolution (256x256).
216
  - After setting the input image path or changed the width/height of the input image, press the "Preview" button to visualize the resized input image.
217
  """
 
243
 
244
  seed_textbox = gr.Textbox(label="Seed", value=-1)
245
  seed_button = gr.Button(value="\U0001F3B2", elem_classes="toolbutton")
246
+ seed_button.click(fn=lambda: gr.Textbox(value=random.randint(1, 1e8)), inputs=[], outputs=[seed_textbox])
247
 
248
 
249
 
 
251
 
252
  with gr.Column():
253
  with gr.Row():
254
+ input_image_path = gr.Textbox(label="Input Image URL", lines=1, scale=10, info="Press Enter or the Preview button to confirm the input image.")
255
  preview_button = gr.Button(value="Preview")
256
 
257
  with gr.Row():
 
286
  pil_image = pil_image.crop((left, top, right, bottom))
287
 
288
  pil_image = pil_image.resize((width_slider, height_slider))
289
+ return gr.Image(value=np.array(pil_image))
290
 
291
  preview_button.click(fn=update_and_resize_image, inputs=[input_image_path, height_slider, width_slider, center_crop], outputs=[input_image])
292
  input_image_path.submit(fn=update_and_resize_image, inputs=[input_image_path, height_slider, width_slider, center_crop], outputs=[input_image])