muhammadsalmanalfaridzi commited on
Commit
aa6b347
β€’
1 Parent(s): 66bafcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -495,18 +495,31 @@ def create_canvas(canvas_size, bg_choice, custom_color):
495
 
496
  # Function to add watermark
497
  def add_watermark(image, watermark_path):
498
- watermark = Image.open(watermark_path).convert("RGBA")
499
- image = image.convert("RGBA")
500
-
501
- # Center the watermark
502
- img_width, img_height = image.size
503
- wm_width, wm_height = watermark.size
504
- x = (img_width - wm_width) // 2
505
- y = (img_height - wm_height) // 2
506
-
507
- image.paste(watermark, (x, y), watermark)
508
  return image
509
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
  def save_image(canvas, output_folder, filename, output_format):
511
  output_ext = 'jpg' if output_format == 'JPG' else 'png'
512
  output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.{output_ext}")
 
495
 
496
  # Function to add watermark
497
  def add_watermark(image, watermark_path):
498
+ if watermark_path:
499
+ watermark = Image.open(watermark_path).convert("RGBA")
500
+ width, height = image.size
501
+ watermark = watermark.resize((width // 4, height // 4), Image.ANTIALIAS) # Resize watermark
502
+ w_width, w_height = watermark.size
503
+
504
+ # Position the watermark in the center
505
+ position = ((width - w_width) // 2, (height - w_height) // 2)
506
+ image.paste(watermark, position, watermark)
 
507
  return image
508
+
509
+ def process_image(image_path, watermark_path):
510
+ try:
511
+ # Menghapus latar belakang
512
+ image_without_bg = remove_background_bria(image_path)
513
+ if image_without_bg is None:
514
+ raise Exception("Background removal failed.")
515
+
516
+ # Menambahkan watermark
517
+ final_image = add_watermark(image_without_bg, watermark_path)
518
+ return final_image
519
+ except Exception as e:
520
+ print(f"Error processing image: {e}")
521
+ return None
522
+
523
  def save_image(canvas, output_folder, filename, output_format):
524
  output_ext = 'jpg' if output_format == 'JPG' else 'png'
525
  output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.{output_ext}")