1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import sys from PIL import Image import numpy as np import random from io import BytesIO import win32clipboard from PIL import Image
def add_noise_and_distort_image(image_path, output_path, noise_level=0.02, distortion_level=5): """ Add random noise and slight distortion to an image """ image = Image.open(image_path) image_np = np.array(image)
noise = np.random.randint(0, 256, image_np.shape, dtype=np.uint8) * noise_level noised_image_np = image_np + noise - (noise * noise_level)
noised_image_np = np.clip(noised_image_np, 0, 255)
rows, cols, ch = noised_image_np.shape for i in range(rows): for j in range(cols): if random.random() < 0.05: dx = random.randint(-distortion_level, distortion_level) dy = random.randint(-distortion_level, distortion_level) new_x, new_y = min(max(j + dx, 0), cols - 1), min(max(i + dy, 0), rows - 1) noised_image_np[i, j] = noised_image_np[new_y, new_x]
distorted_image = Image.fromarray(noised_image_np.astype('uint8')) distorted_image.save(output_path)
def send_to_clipboard(clip_type, data): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(clip_type, data) win32clipboard.CloseClipboard()
if __name__ == "__main__": if len(sys.argv) != 5: print("Usage: python add_noise.py <input_image_path> <output_image_path> <noise_level> <distortion_level>") sys.exit(1)
input_image_path = sys.argv[1] output_image_path = sys.argv[2] noise_level = float(sys.argv[3]) distortion_level = int(sys.argv[4])
add_noise_and_distort_image(input_image_path, output_image_path, noise_level, distortion_level)
image = Image.open(output_image_path) output = BytesIO() image.convert("RGB").save(output, "BMP") data = output.getvalue()[14:] output.close() send_to_clipboard(win32clipboard.CF_DIB, data)
|