YoadTew commited on
Commit
14f0001
·
1 Parent(s): 5b9bac1

Update logic to resize and crop to 1024x1024

Browse files
Files changed (1) hide show
  1. app.py +70 -1
app.py CHANGED
@@ -22,6 +22,7 @@ from addit_methods import add_object_generated, add_object_real
22
  # Global variables for model
23
  pipe = None
24
  device = None
 
25
 
26
  # Initialize model at startup
27
  print("Initializing ADDIT model...")
@@ -64,6 +65,60 @@ def validate_inputs(prompt_source, prompt_target, subject_token):
64
  return f"Subject token '{subject_token}' must appear in the target prompt"
65
  return None
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  @spaces.GPU
68
  def process_generated_image(
69
  prompt_source,
@@ -161,6 +216,8 @@ def process_real_image(
161
  # Print current time and input information
162
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
163
  print(f"\n[{current_time}] Starting Real Image Processing")
 
 
164
  print(f"Source Image Size: {source_image.size}")
165
  print(f"Source Prompt: '{prompt_source}'")
166
  print(f"Target Prompt: '{prompt_target}'")
@@ -319,10 +376,11 @@ def create_interface():
319
  # Real Images Tab
320
  with gr.TabItem("📸 Real Images"):
321
  gr.Markdown("### Upload an image and add objects to it")
322
- gr.HTML("<p style='color: red; font-weight: bold; margin: -15px -10px;'>Note: Images will be resized to 1024x1024 pixels. For best results, use square images.</p>")
323
 
324
  with gr.Row():
325
  with gr.Column(scale=1):
 
326
  real_source_image = gr.Image(label="Source Image", type="pil")
327
  real_prompt_source = gr.Textbox(
328
  label="Source Prompt",
@@ -385,6 +443,17 @@ def create_interface():
385
  real_edited_output = gr.Image(label="Edited Image", type="pil")
386
  real_status = gr.Textbox(label="Status", interactive=False)
387
 
 
 
 
 
 
 
 
 
 
 
 
388
  real_submit_btn.click(
389
  fn=process_real_image,
390
  inputs=[
 
22
  # Global variables for model
23
  pipe = None
24
  device = None
25
+ original_image_size = None
26
 
27
  # Initialize model at startup
28
  print("Initializing ADDIT model...")
 
65
  return f"Subject token '{subject_token}' must appear in the target prompt"
66
  return None
67
 
68
+ def resize_and_crop_image(image):
69
+ """
70
+ Resize and center crop image to 1024x1024.
71
+ Returns the processed image, a message about what was done, and original size info.
72
+ """
73
+ if image is None:
74
+ return None, "", None
75
+
76
+ original_width, original_height = image.size
77
+ original_size = (original_width, original_height)
78
+
79
+ # If already 1024x1024, no processing needed
80
+ if original_width == 1024 and original_height == 1024:
81
+ return image, "", original_size
82
+
83
+ # Calculate scaling to make smaller dimension 1024
84
+ scale = 1024 / min(original_width, original_height)
85
+ new_width = int(original_width * scale)
86
+ new_height = int(original_height * scale)
87
+
88
+ # Resize image
89
+ resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
90
+
91
+ # Center crop to 1024x1024
92
+ left = (new_width - 1024) // 2
93
+ top = (new_height - 1024) // 2
94
+ right = left + 1024
95
+ bottom = top + 1024
96
+
97
+ cropped_image = resized_image.crop((left, top, right, bottom))
98
+
99
+ # Create status message
100
+ if new_width == 1024 and new_height == 1024:
101
+ message = f"<div style='background-color: #e8f5e8; border: 1px solid #4caf50; border-radius: 5px; padding: 8px; margin-bottom: 10px;'><span style='color: #2e7d32; font-weight: bold;'>✅ Image resized to 1024×1024</span></div>"
102
+ else:
103
+ message = f"<div style='background-color: #e8f5e8; border: 1px solid #4caf50; border-radius: 5px; padding: 8px; margin-bottom: 10px;'><span style='color: #2e7d32; font-weight: bold;'>✅ Image resized and center cropped to 1024×1024</span></div>"
104
+
105
+ return cropped_image, message, original_size
106
+
107
+ def handle_image_upload(image):
108
+ """Handle image upload and store original size globally"""
109
+ global original_image_size
110
+
111
+ if image is None:
112
+ original_image_size = None
113
+ return None, ""
114
+
115
+ # Store original size
116
+ original_image_size = image.size
117
+
118
+ # Process image
119
+ processed_image, message, _ = resize_and_crop_image(image)
120
+ return processed_image, message
121
+
122
  @spaces.GPU
123
  def process_generated_image(
124
  prompt_source,
 
216
  # Print current time and input information
217
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
218
  print(f"\n[{current_time}] Starting Real Image Processing")
219
+ if original_image_size:
220
+ print(f"Original uploaded image size: {original_image_size[0]}×{original_image_size[1]}")
221
  print(f"Source Image Size: {source_image.size}")
222
  print(f"Source Prompt: '{prompt_source}'")
223
  print(f"Target Prompt: '{prompt_target}'")
 
376
  # Real Images Tab
377
  with gr.TabItem("📸 Real Images"):
378
  gr.Markdown("### Upload an image and add objects to it")
379
+ gr.HTML("<p style='color: orange; font-weight: bold; margin: -15px -10px;'>Note: Images will be automatically resized and center cropped to 1024×1024 pixels.</p>")
380
 
381
  with gr.Row():
382
  with gr.Column(scale=1):
383
+ real_image_status = gr.HTML(visible=False)
384
  real_source_image = gr.Image(label="Source Image", type="pil")
385
  real_prompt_source = gr.Textbox(
386
  label="Source Prompt",
 
443
  real_edited_output = gr.Image(label="Edited Image", type="pil")
444
  real_status = gr.Textbox(label="Status", interactive=False)
445
 
446
+ # Handle image upload and preprocessing
447
+ real_source_image.upload(
448
+ fn=handle_image_upload,
449
+ inputs=[real_source_image],
450
+ outputs=[real_source_image, real_image_status]
451
+ ).then(
452
+ fn=lambda status: gr.update(visible=bool(status.strip()), value=status),
453
+ inputs=[real_image_status],
454
+ outputs=[real_image_status]
455
+ )
456
+
457
  real_submit_btn.click(
458
  fn=process_real_image,
459
  inputs=[