Untitled
user_2381398
plain_text
2 years ago
1.8 kB
6
Indexable
import cv2 import numpy as np import os import random # Step 1: Load images image_path = "./image" out_noise_path = "./noise_image" #check output path integrity if not os.path.exists(out_noise_path): os.mkdir(out_noise_path) def pixelate_image(image, scale_percent = 10): width = int(image.shape[1] * scale_percent / 100) height = int(image.shape[0] * scale_percent / 100) dim = (width, height) small_image = cv2.resize(image, dim, interpolation = cv2.INTER_LINEAR) # scale back to original size width = int(small_image.shape[1] * 100 / scale_percent) height = int(small_image.shape[0] * 100 / scale_percent) dim = (width, height) low_res_image = cv2.resize(small_image, dim, interpolation = cv2.INTER_NEAREST) return low_res_image def add_random_noise(image, num_pixels=1000): # Get image dimensions height, width, _ = image.shape # Pick random pixels to add noise to for _ in range(num_pixels): y_coord=random.randint(0, height - 1) x_coord=random.randint(0, width - 1) image[y_coord][x_coord] = 255 return image for image_path1 in os.listdir(image_path): for image_path2 in os.listdir(image_path+"/"+image_path1): image = cv2.imread(image_path+"/"+image_path1+"/"+image_path2) # Step 2: Pixelate and add noise to image pixelated_image = pixelate_image(image) noisy_image = add_random_noise(pixelated_image) # Step 3: Save the noisy image #check path integrity if not os.path.exists(out_noise_path+"/"+image_path1): os.mkdir(out_noise_path+"/"+image_path1) cv2.imwrite(out_noise_path+"/"+image_path1+"/"+image_path2, noisy_image)
Editor is loading...