omthakur1 commited on
Commit
f647b99
·
1 Parent(s): 15c1025

Support quality parameter (Standard/HD/Ultra) with zero PNG compression for maximum quality

Browse files
Files changed (1) hide show
  1. app.py +35 -5
app.py CHANGED
@@ -58,8 +58,9 @@ def health_check():
58
  @app.route('/remove-background', methods=['POST'])
59
  def remove_background():
60
  """
61
- Background removal using rembg with u2netp model
62
  NO external APIs - 100% self-hosted!
 
63
  """
64
  try:
65
  # Get uploaded file
@@ -67,10 +68,31 @@ def remove_background():
67
  return {'error': 'No image file provided'}, 400
68
 
69
  file = request.files['image']
70
- input_image = Image.open(file.stream)
 
 
 
71
 
72
  print(f"🎨 Processing image: {file.filename}")
73
- print(f"📐 Size: {input_image.size}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  # Get session and remove background
76
  session = get_session()
@@ -78,11 +100,19 @@ def remove_background():
78
 
79
  print(f"✅ Background removed successfully!")
80
 
81
- # Convert to PNG bytes
82
  img_io = io.BytesIO()
83
- output_image.save(img_io, 'PNG', optimize=False)
 
 
 
 
 
84
  img_io.seek(0)
85
 
 
 
 
86
  return send_file(
87
  img_io,
88
  mimetype='image/png',
 
58
  @app.route('/remove-background', methods=['POST'])
59
  def remove_background():
60
  """
61
+ Background removal using rembg with u2net model
62
  NO external APIs - 100% self-hosted!
63
+ Supports quality options: standard, hd, ultra
64
  """
65
  try:
66
  # Get uploaded file
 
68
  return {'error': 'No image file provided'}, 400
69
 
70
  file = request.files['image']
71
+ input_image = Image.open(file.stream).convert('RGBA')
72
+
73
+ # Get quality setting (default: ultra)
74
+ quality = request.form.get('quality', 'ultra').lower()
75
 
76
  print(f"🎨 Processing image: {file.filename}")
77
+ print(f"📐 Original size: {input_image.size}")
78
+ print(f"🎯 Quality mode: {quality}")
79
+
80
+ # Resize based on quality (preserve aspect ratio)
81
+ max_dimensions = {
82
+ 'standard': 1920, # Full HD
83
+ 'hd': 2560, # 2K
84
+ 'ultra': 3840, # 4K
85
+ '4k': 3840 # 4K alias
86
+ }
87
+
88
+ max_size = max_dimensions.get(quality, 3840) # Default to 4K
89
+
90
+ # Only resize if image is larger than max
91
+ if max(input_image.size) > max_size:
92
+ ratio = max_size / max(input_image.size)
93
+ new_size = tuple(int(dim * ratio) for dim in input_image.size)
94
+ input_image = input_image.resize(new_size, Image.Resampling.LANCZOS)
95
+ print(f"📐 Resized to: {input_image.size}")
96
 
97
  # Get session and remove background
98
  session = get_session()
 
100
 
101
  print(f"✅ Background removed successfully!")
102
 
103
+ # Save with MAXIMUM quality - no compression
104
  img_io = io.BytesIO()
105
+ output_image.save(
106
+ img_io,
107
+ 'PNG',
108
+ compress_level=0, # No compression (0-9, 0=no compression)
109
+ optimize=False # Don't optimize file size
110
+ )
111
  img_io.seek(0)
112
 
113
+ output_size_mb = len(img_io.getvalue()) / (1024 * 1024)
114
+ print(f"📦 Output size: {output_size_mb:.2f} MB")
115
+
116
  return send_file(
117
  img_io,
118
  mimetype='image/png',