muhammadsalmanalfaridzi commited on
Commit
5ba039a
β€’
1 Parent(s): aa6b347

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -23
app.py CHANGED
@@ -494,31 +494,40 @@ def create_canvas(canvas_size, bg_choice, custom_color):
494
  return Image.new("RGBA", canvas_size, (0, 0, 0, 0))
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'
@@ -590,6 +599,65 @@ def process_images(input_files, bg_method='rembg', watermark_path=None, canvas_s
590
  print(f"Error processing image {future_to_image[future]}: {e}")
591
 
592
  output_zip_path = "processed_images.zip"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
  with zipfile.ZipFile(output_zip_path, 'w') as zipf:
594
  for file, _ in processed_images:
595
  zipf.write(file, os.path.basename(file))
 
494
  return Image.new("RGBA", canvas_size, (0, 0, 0, 0))
495
 
496
  # Function to add watermark
497
+ def generate_image(prompt):
498
+ # Code to generate an image using Stable Diffusion
499
+ # Placeholder: Replace with actual image generation code
500
+ response = requests.get("URL_OF_GENERATED_IMAGE") # Replace with the actual URL or generation method
501
+ img = Image.open(BytesIO(response.content))
502
+ return img
503
+
504
  def add_watermark(image, watermark_path):
505
+ watermark = Image.open(watermark_path).convert("RGBA")
506
+
507
+ # Resize watermark if needed
508
+ watermark = watermark.resize((image.width // 4, image.height // 4), Image.ANTIALIAS)
509
+
510
+ # Position watermark in the center
511
+ position = ((image.width - watermark.width) // 2, (image.height - watermark.height) // 2)
512
+
513
+ # Composite the watermark onto the image
514
+ transparent = Image.new('RGBA', image.size, (0, 0, 0, 0))
515
+ transparent.paste(image, (0, 0))
516
+ transparent.paste(watermark, position, watermark)
517
+
518
+ return transparent
519
 
520
+ def process_image(prompt, watermark_path, bg_removal_choice):
521
+ image = generate_image(prompt)
522
+
523
+ if bg_removal_choice == "none":
524
+ final_image = image
525
+ else:
526
+ # Placeholder for actual background removal code
527
+ final_image = image # Replace with background removal logic
528
+
529
+ final_image_with_watermark = add_watermark(final_image, watermark_path)
530
+ return final_image_with_watermark
 
 
531
 
532
  def save_image(canvas, output_folder, filename, output_format):
533
  output_ext = 'jpg' if output_format == 'JPG' else 'png'
 
599
  print(f"Error processing image {future_to_image[future]}: {e}")
600
 
601
  output_zip_path = "processed_images.zip"
602
+ # Handle uploaded files
603
+ if isinstance(input_files, list):
604
+ images = [Image.open(file.name) for file in input_files]
605
+ else:
606
+ with zipfile.ZipFile(input_files.name, 'r') as zip_ref:
607
+ zip_ref.extractall("extracted_images")
608
+ images = [Image.open(os.path.join("extracted_images", f)) for f in os.listdir("extracted_images")]
609
+
610
+ # Background removal logic (placeholder)
611
+ for img in images:
612
+ # Implement background removal based on selected method
613
+ if bg_method == "rembg":
614
+ img = remove_background_with_rembg(img) # Placeholder function
615
+ elif bg_method == "bria":
616
+ img = remove_background_with_bria(img) # Placeholder function
617
+
618
+ # Add watermark if provided
619
+ if watermark:
620
+ watermark_img = Image.open(watermark.name).convert("RGBA")
621
+ img = add_watermark(img, watermark_img)
622
+
623
+ # Save processed image
624
+ output_path = f"processed_{len(processed_images)}.{output_format.lower()}"
625
+ img.save(output_path, format=output_format)
626
+ processed_images.append(output_path)
627
+
628
+ # Create a zip file of processed images
629
+ with zipfile.ZipFile(output_zip_path, 'w') as zipf:
630
+ for processed_img in processed_images:
631
+ zipf.write(processed_img)
632
+
633
+ return processed_images, output_zip_path
634
+
635
+ def add_watermark(image, watermark):
636
+ # Resize and position watermark
637
+ watermark = watermark.resize((image.width // 4, image.height // 4), Image.ANTIALIAS)
638
+ position = ((image.width - watermark.width) // 2, (image.height - watermark.height) // 2)
639
+
640
+ transparent = Image.new('RGBA', image.size, (0, 0, 0, 0))
641
+ transparent.paste(image.convert("RGBA"), (0, 0))
642
+ transparent.paste(watermark, position, watermark)
643
+
644
+ return transparent.convert("RGB")
645
+
646
+ def remove_background_with_rembg(image):
647
+ # Placeholder for actual background removal code
648
+ return image # Replace with actual removal logic
649
+
650
+ def remove_background_with_bria(image):
651
+ # Placeholder for actual background removal code
652
+ return image # Replace with actual removal logic
653
+
654
+ def show_color_picker(bg_choice):
655
+ return bg_choice == "custom"
656
+
657
+ def update_compare(selected_image):
658
+ # Logic to display original vs processed images
659
+ return selected_image, selected_image # Replace with actual images
660
+
661
  with zipfile.ZipFile(output_zip_path, 'w') as zipf:
662
  for file, _ in processed_images:
663
  zipf.write(file, os.path.basename(file))