Snipaste 自动给截图添加噪点

此功能需要用到Pro版的自定义命令功能

新建Python文件, 内容如下

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 """
# 打开图像并转换为numpy数组
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: # 随机选择5%的像素进行扭曲
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_path = r'output.png' # 图片绝对路径
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)

命名为add_noise.pyw, 注意, 需要使用pip下载相关依赖

在Snipaste中, 添加自定义命令

1
snip -o "D:\Temp\add_noise_input.png";exec(cmd /c "D:\Temp\add_noise.pyw" "D:\Temp\add_noise_input.png" "D:\Temp\add_noise_output.png" 0.5 5);

指定快捷键即可使用

作者

cuicui

发布于

2023-11-22

更新于

2023-11-22

许可协议

评论