mxpln commited on
Commit
919ed9c
·
1 Parent(s): 75edc1c

Preserve input resolution and ensure PNG output at 100% quality

Browse files
Files changed (2) hide show
  1. app.py +7 -10
  2. qwenimage/pipeline_qwenimage_edit_plus.py +16 -2
app.py CHANGED
@@ -478,21 +478,18 @@ def infer(
478
  except Exception:
479
  continue
480
 
481
- # Adjust dimensions to preserve aspect ratio and ensure quality
482
  if len(pil_images) > 0:
483
  input_width, input_height = pil_images[0].size
484
  aspect_ratio = input_width / input_height
485
  else:
486
  aspect_ratio = 1.0 # Default square if no image
487
 
488
- # Adjust dimensions to preserve aspect ratio
489
  if height is None and width is None:
490
- # Use default calculation preserving ratio
491
- target_area = 1024 * 1024
492
- width_calc = math.sqrt(target_area * aspect_ratio)
493
- height_calc = width_calc / aspect_ratio
494
- width = round(width_calc / 32) * 32
495
- height = round(height_calc / 32) * 32
496
  elif height is not None and width is None:
497
  width = round(height * aspect_ratio / 32) * 32
498
  elif width is not None and height is None:
@@ -529,11 +526,11 @@ def infer(
529
  num_images_per_prompt=num_images_per_prompt,
530
  ).images
531
 
532
- # Save images as PNG with max quality
533
  output_paths = []
534
  for i, img in enumerate(image):
535
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
536
- img.save(tmp_file.name, format="PNG")
537
  output_paths.append(tmp_file.name)
538
 
539
  # Return paths, seed, and make button visible
 
478
  except Exception:
479
  continue
480
 
481
+ # Adjust dimensions to preserve input resolution and ensure quality
482
  if len(pil_images) > 0:
483
  input_width, input_height = pil_images[0].size
484
  aspect_ratio = input_width / input_height
485
  else:
486
  aspect_ratio = 1.0 # Default square if no image
487
 
488
+ # Preserve input resolution by default - use input dimensions directly
489
  if height is None and width is None:
490
+ # Use input dimensions directly, rounded to required multiple
491
+ width = round(input_width / 32) * 32
492
+ height = round(input_height / 32) * 32
 
 
 
493
  elif height is not None and width is None:
494
  width = round(height * aspect_ratio / 32) * 32
495
  elif width is not None and height is None:
 
526
  num_images_per_prompt=num_images_per_prompt,
527
  ).images
528
 
529
+ # Save images as PNG with 100% quality (lossless, no compression)
530
  output_paths = []
531
  for i, img in enumerate(image):
532
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
533
+ img.save(tmp_file.name, format="PNG", compress_level=0, optimize=False)
534
  output_paths.append(tmp_file.name)
535
 
536
  # Return paths, seed, and make button visible
qwenimage/pipeline_qwenimage_edit_plus.py CHANGED
@@ -627,8 +627,18 @@ class QwenImageEditPlusPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin):
627
  [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When
628
  returning a tuple, the first element is a list with the generated images.
629
  """
 
630
  image_size = image[-1].size if isinstance(image, list) else image.size
631
- calculated_width, calculated_height = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1])
 
 
 
 
 
 
 
 
 
632
  height = height or calculated_height
633
  width = width or calculated_width
634
 
@@ -677,7 +687,11 @@ class QwenImageEditPlusPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin):
677
  condition_width, condition_height = calculate_dimensions(
678
  CONDITION_IMAGE_SIZE, image_width / image_height
679
  )
680
- vae_width, vae_height = calculate_dimensions(VAE_IMAGE_SIZE, image_width / image_height)
 
 
 
 
681
  condition_image_sizes.append((condition_width, condition_height))
682
  vae_image_sizes.append((vae_width, vae_height))
683
  condition_images.append(self.image_processor.resize(img, condition_height, condition_width))
 
627
  [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When
628
  returning a tuple, the first element is a list with the generated images.
629
  """
630
+ # Preserve input resolution - use input dimensions directly
631
  image_size = image[-1].size if isinstance(image, list) else image.size
632
+ input_width, input_height = image_size[0], image_size[1]
633
+ aspect_ratio = input_width / input_height
634
+
635
+ # Use input dimensions if not specified, rounded to required multiple
636
+ if height is None and width is None:
637
+ calculated_width = round(input_width / (self.vae_scale_factor * 2)) * (self.vae_scale_factor * 2)
638
+ calculated_height = round(input_height / (self.vae_scale_factor * 2)) * (self.vae_scale_factor * 2)
639
+ else:
640
+ calculated_width, calculated_height = calculate_dimensions(1024 * 1024, aspect_ratio)
641
+
642
  height = height or calculated_height
643
  width = width or calculated_width
644
 
 
687
  condition_width, condition_height = calculate_dimensions(
688
  CONDITION_IMAGE_SIZE, image_width / image_height
689
  )
690
+ # Use input dimensions for VAE processing to preserve quality
691
+ # Round to required multiple for VAE compatibility
692
+ multiple_of = self.vae_scale_factor * 2
693
+ vae_width = round(image_width / multiple_of) * multiple_of
694
+ vae_height = round(image_height / multiple_of) * multiple_of
695
  condition_image_sizes.append((condition_width, condition_height))
696
  vae_image_sizes.append((vae_width, vae_height))
697
  condition_images.append(self.image_processor.resize(img, condition_height, condition_width))