comrender commited on
Commit
51b5fb9
Β·
verified Β·
1 Parent(s): dc2fafb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -44,6 +44,11 @@ MAX_SEED = 1000000
44
  MAX_PIXEL_BUDGET = 8192 * 8192 # Increased for tiling support
45
 
46
 
 
 
 
 
 
47
  def process_input(input_image, upscale_factor):
48
  """Process input image and handle size constraints"""
49
  w, h = input_image.size
@@ -101,6 +106,8 @@ def tiled_flux_img2img(pipe, prompt, image, strength, steps, guidance, generator
101
  for y in range(0, h, tile_size - overlap):
102
  tile_w = min(tile_size, w - x)
103
  tile_h = min(tile_size, h - y)
 
 
104
  tile = image.crop((x, y, x + tile_w, y + tile_h))
105
 
106
  # Run Flux on tile
@@ -240,6 +247,13 @@ def enhance_image(
240
  w, h = input_image.size
241
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor), resample=Image.LANCZOS)
242
 
 
 
 
 
 
 
 
243
  # Tiled Flux Img2Img for refinement
244
  image = tiled_flux_img2img(
245
  pipe,
@@ -253,9 +267,14 @@ def enhance_image(
253
  overlap=32
254
  )
255
 
 
 
 
 
 
256
  if was_resized:
257
- gr.Info(f"πŸ“ Resizing output to target size: {w_original * upscale_factor}x{h_original * upscale_factor}")
258
- image = image.resize((w_original * upscale_factor, h_original * upscale_factor), resample=Image.LANCZOS)
259
 
260
  # Resize input image to match output size for slider alignment
261
  resized_input = true_input_image.resize(image.size, resample=Image.LANCZOS)
@@ -469,4 +488,5 @@ with gr.Blocks(css=css, title="🎨 AI Image Upscaler - FLUX") as demo:
469
  """)
470
 
471
  if __name__ == "__main__":
472
- demo.queue().launch(share=True, server_name="0.0.0.0", server_port=7860)
 
 
44
  MAX_PIXEL_BUDGET = 8192 * 8192 # Increased for tiling support
45
 
46
 
47
+ def make_divisible_by_16(size):
48
+ """Adjust size to nearest multiple of 16, stretching if necessary"""
49
+ return ((size // 16) * 16) if (size % 16) < 8 else ((size // 16 + 1) * 16)
50
+
51
+
52
  def process_input(input_image, upscale_factor):
53
  """Process input image and handle size constraints"""
54
  w, h = input_image.size
 
106
  for y in range(0, h, tile_size - overlap):
107
  tile_w = min(tile_size, w - x)
108
  tile_h = min(tile_size, h - y)
109
+ if tile_h < 16 or tile_w < 16: # Skip tiny tiles
110
+ continue
111
  tile = image.crop((x, y, x + tile_w, y + tile_h))
112
 
113
  # Run Flux on tile
 
247
  w, h = input_image.size
248
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor), resample=Image.LANCZOS)
249
 
250
+ # Resize control_image to divisible by 16 (stretching)
251
+ control_w, control_h = control_image.size
252
+ new_control_w = make_divisible_by_16(control_w)
253
+ new_control_h = make_divisible_by_16(control_h)
254
+ if (new_control_w, new_control_h) != (control_w, control_h):
255
+ control_image = control_image.resize((new_control_w, new_control_h), resample=Image.LANCZOS)
256
+
257
  # Tiled Flux Img2Img for refinement
258
  image = tiled_flux_img2img(
259
  pipe,
 
267
  overlap=32
268
  )
269
 
270
+ # Resize back to original target size if stretched
271
+ target_w, target_h = w_original * upscale_factor, h_original * upscale_factor
272
+ if image.size != (target_w, target_h):
273
+ image = image.resize((target_w, target_h), resample=Image.LANCZOS)
274
+
275
  if was_resized:
276
+ gr.Info(f"πŸ“ Resizing output to target size: {target_w}x{target_h}")
277
+ image = image.resize((target_w, target_h), resample=Image.LANCZOS)
278
 
279
  # Resize input image to match output size for slider alignment
280
  resized_input = true_input_image.resize(image.size, resample=Image.LANCZOS)
 
488
  """)
489
 
490
  if __name__ == "__main__":
491
+ demo.queue().launch(share=True, server_name="0.0.0.0", server_port=7860)
492
+ </xai:function_call