Mehedi258456 commited on
Commit
d05783f
·
verified ·
1 Parent(s): 10f6dff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -4,59 +4,54 @@ from rembg import remove
4
  import tempfile
5
 
6
  MAX_MP = 100_000_000 # 100 Megapixels
7
- MIN_MP = 4_000_000 # 4 Megapixels
8
 
9
- def resize_to_target_mp(img, target_mp):
10
- orig_w, orig_h = img.size
11
- orig_pixels = orig_w * orig_h
 
12
 
13
- target_pixels = target_mp
14
- if orig_pixels == target_pixels:
15
- return img # already matches
 
 
16
 
17
- scale = (target_pixels / orig_pixels) ** 0.5
18
- new_size = (int(orig_w * scale), int(orig_h * scale))
19
-
20
- # If new size is too big, cap it at MAX_MP
21
- final_pixels = new_size[0] * new_size[1]
22
- if final_pixels > MAX_MP:
23
- scale = (MAX_MP / orig_pixels) ** 0.5
24
- new_size = (int(orig_w * scale), int(orig_h * scale))
25
-
26
- return img.resize(new_size, Image.LANCZOS)
27
-
28
- def process_image(image, target_mp):
29
  target_pixels = int(target_mp * 1_000_000)
30
 
31
- # JPG: original bg
32
- resized_jpg = resize_to_target_mp(image, target_pixels)
 
 
 
 
33
 
34
- # PNG: remove bg then resize
35
- transparent = remove(image)
36
- resized_png = resize_to_target_mp(transparent, target_pixels)
37
 
38
- # Save PNG
39
  png_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
40
- resized_png.save(png_file.name, format="PNG")
41
 
42
- # Save JPG
43
  jpg_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
44
- resized_jpg.convert("RGB").save(jpg_file.name, format="JPEG", quality=95)
45
 
46
  return png_file.name, jpg_file.name
47
 
48
- title = "Background Remover + Upscaler by Megapixel (4MP–100MP)"
49
  description = (
50
- "Upload an image (JPG or PNG). Select desired resolution in Megapixels (MP). "
51
- "You'll get 2 downloads: a transparent PNG (background removed) and a JPG with original background. "
52
- "Maximum allowed output: 100 Megapixels."
53
  )
54
 
55
  interface = gr.Interface(
56
  fn=process_image,
57
  inputs=[
58
  gr.Image(type="pil", label="Upload Image"),
59
- gr.Slider(4, 100, step=1, label="Target Resolution (in Megapixels)")
 
60
  ],
61
  outputs=[
62
  gr.File(label="Download Transparent PNG"),
 
4
  import tempfile
5
 
6
  MAX_MP = 100_000_000 # 100 Megapixels
 
7
 
8
+ def limit_to_max_mp(img, target_mp):
9
+ max_pixels = int(target_mp * 1_000_000)
10
+ w, h = img.size
11
+ total_pixels = w * h
12
 
13
+ if total_pixels > max_pixels:
14
+ scale = (max_pixels / total_pixels) ** 0.5
15
+ new_size = (int(w * scale), int(h * scale))
16
+ return img.resize(new_size, Image.LANCZOS)
17
+ return img
18
 
19
+ def process_image(image, scale_factor, target_mp):
 
 
 
 
 
 
 
 
 
 
 
20
  target_pixels = int(target_mp * 1_000_000)
21
 
22
+ # Step 1: Scale original image
23
+ w, h = image.size
24
+ new_w, new_h = int(w * scale_factor), int(h * scale_factor)
25
+
26
+ upscaled_img = image.resize((new_w, new_h), Image.BICUBIC)
27
+ transparent_img = remove(image).resize((new_w, new_h), Image.LANCZOS)
28
 
29
+ # Step 2: Enforce max MP
30
+ final_jpg = limit_to_max_mp(upscaled_img, target_mp)
31
+ final_png = limit_to_max_mp(transparent_img, target_mp)
32
 
33
+ # Step 3: Save PNG (transparent)
34
  png_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
35
+ final_png.save(png_file.name, format="PNG")
36
 
37
+ # Step 4: Save JPG (original bg)
38
  jpg_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
39
+ final_jpg.convert("RGB").save(jpg_file.name, format="JPEG", quality=95)
40
 
41
  return png_file.name, jpg_file.name
42
 
43
+ title = "Image Upscaler + Background Remover (2x8x & 4–100MP)"
44
  description = (
45
+ "Upload an image. Select upscale factor (2x to 8x) and max resolution (in Megapixels). "
46
+ "You'll get 2 files: Transparent PNG (BG removed) and JPG (original background)."
 
47
  )
48
 
49
  interface = gr.Interface(
50
  fn=process_image,
51
  inputs=[
52
  gr.Image(type="pil", label="Upload Image"),
53
+ gr.Slider(2, 8, step=1, label="Upscale Factor (2x to 8x)"),
54
+ gr.Slider(4, 100, step=1, label="Maximum Output Resolution (in Megapixels)")
55
  ],
56
  outputs=[
57
  gr.File(label="Download Transparent PNG"),