Advertisement
jarekmor

logo_remove

Sep 3rd, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4.  
  5. # Set the directory where images are stored
  6. image_dir = 'path_to_images/'
  7. output_dir = 'path_to_output/'
  8.  
  9. # Load one image to manually get the logo coordinates
  10. sample_image = cv2.imread(os.path.join(image_dir, 'sample_image.jpg'))
  11. # Set the coordinates of the logo (x, y, width, height)
  12. logo_coords = (x, y, w, h)
  13.  
  14. # Create a mask for the logo
  15. mask = np.zeros_like(sample_image[:, :, 0])  # Same size as image, single channel
  16. cv2.rectangle(mask, (logo_coords[0], logo_coords[1]),
  17.               (logo_coords[0] + logo_coords[2], logo_coords[1] + logo_coords[3]),
  18.               255, -1)
  19.  
  20. # Function to remove logo using inpainting
  21. def remove_logo(image, mask):
  22.     # Inpaint the area of the logo
  23.     result = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)
  24.     return result
  25.  
  26. # Process all images
  27. for filename in os.listdir(image_dir):
  28.     if filename.endswith('.jpg') or filename.endswith('.png'):
  29.         img = cv2.imread(os.path.join(image_dir, filename))
  30.         result = remove_logo(img, mask)
  31.         cv2.imwrite(os.path.join(output_dir, filename), result)
  32.  
  33. print("Logo removal completed for all images.")
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement