muhammadsalmanalfaridzi commited on
Commit
0a14b10
β€’
1 Parent(s): 2d8dabe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -31,10 +31,23 @@ def remove_background_rembg(input_path):
31
  return img
32
 
33
  def remove_background_bria(input_path):
34
- print(f"Removing background using bria for image: {input_path}")
35
  pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True, device=0)
36
- pillow_image = pipe(input_path)
37
- return pillow_image
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Fungsi untuk memproses gambar menggunakan prompt
40
  def text_to_image(prompt):
@@ -403,6 +416,13 @@ def process_single_image(image_path, output_folder, bg_method, canvas_size_name,
403
  image_with_no_bg = remove_background_rembg(image_path) # Placeholder for existing function
404
  elif bg_method == 'bria':
405
  image_with_no_bg = remove_background_bria(image_path) # Placeholder for existing function
 
 
 
 
 
 
 
406
 
407
  temp_image_path = os.path.join(output_folder, f"temp_{filename}")
408
  image_with_no_bg.save(temp_image_path, format='PNG')
 
31
  return img
32
 
33
  def remove_background_bria(input_path):
34
+ image = Image.open(input_path)
35
  pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True, device=0)
36
+ result = pipe(image)
37
+ output_image = result[0]['segmentation']
38
+ output_image = Image.fromarray(output_image)
39
+ return output_image
40
+
41
+ # Fungsi untuk menambahkan watermark
42
+ def add_watermark(original_image, watermark_image, position=(0, 0), transparency=128):
43
+ watermark = watermark_image.convert("RGBA")
44
+ combined = Image.new("RGBA", original_image.size)
45
+
46
+ mask = watermark.split()[3].point(lambda i: i * transparency / 255.0)
47
+ combined.paste(original_image, (0, 0))
48
+ combined.paste(watermark, position, mask)
49
+
50
+ return combined
51
 
52
  # Fungsi untuk memproses gambar menggunakan prompt
53
  def text_to_image(prompt):
 
416
  image_with_no_bg = remove_background_rembg(image_path) # Placeholder for existing function
417
  elif bg_method == 'bria':
418
  image_with_no_bg = remove_background_bria(image_path) # Placeholder for existing function
419
+ # Memuat gambar watermark
420
+ watermark = Image.open(watermark_path).convert("RGBA")
421
+
422
+ # Menambahkan watermark ke gambar asli
423
+ final_image = add_watermark(original_image, watermark, position=(50, 50), transparency=128)
424
+
425
+ return final_image
426
 
427
  temp_image_path = os.path.join(output_folder, f"temp_{filename}")
428
  image_with_no_bg.save(temp_image_path, format='PNG')