Untitled
user_2381398
plain_text
2 years ago
1.4 kB
5
Indexable
import cv2 import numpy as np import os # Step 1: Define the path of your images image_folder_path = './image/' # Get the list of all image paths in the folder image_paths = [os.path.join(image_folder_path, fname) for fname in os.listdir(image_folder_path)] for image_path in image_paths: # Load the image image = cv2.imread(image_path) # Check if image loading was successful if image is None: print(f"Failed to load image at path: {image_path}") continue # Step 2: Add noise to image # Define the noise shape here noise_shape = (100, 100) # Create a black image with the same size as the original image noise = np.zeros_like(image) # Create a white square at the center of the image cv2.rectangle(noise, ((image.shape[1] - noise_shape[1]) // 2, (image.shape[0] - noise_shape[0]) // 2), ((image.shape[1] + noise_shape[1]) // 2, (image.shape[0] + noise_shape[0]) // 2), (255, 255, 255), -1) # Add the noise to the original image noisy_image = cv2.add(image, noise) # Step 3: Save the noisy image # You need to replace 'path_to_save_your_noisy_images/' with the path where you want to save the noisy images cv2.imwrite('path_to_save_your_noisy_images/' + os.path.basename(image_path), noisy_image)
Editor is loading...