-
Notifications
You must be signed in to change notification settings - Fork 0
/
augment_by_gpt.py
52 lines (38 loc) · 1.27 KB
/
augment_by_gpt.py
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
from PIL import Image, ImageEnhance, ImageOps
import random
# 定义数据增强函数
def augment_data(image):
# 随机旋转
angle = random.randint(-10, 10)
image = image.rotate(angle)
# 随机水平翻转
if random.random() < 0.5:
image = ImageOps.flip(image)
# 随机垂直翻转
if random.random() < 0.5:
image = ImageOps.mirror(image)
# 随机裁剪
w, h = image.size
left = random.randint(0, int(w * 0.2))
top = random.randint(0, int(h * 0.2))
right = random.randint(int(w * 0.8), w)
bottom = random.randint(int(h * 0.8), h)
image = image.crop((left, top, right, bottom))
# 随机调整亮度、对比度、饱和度
enhancer = ImageEnhance.Brightness(image)
image = enhancer.enhance(random.uniform(0.8, 1.2))
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(random.uniform(0.8, 1.2))
enhancer = ImageEnhance.Color(image)
image = enhancer.enhance(random.uniform(0.8, 1.2))
return image
if __name__ == '__main__':
# 示例代码
image_path = "example.jpg"
image = Image.open(image_path)
# 展示原图
image.show()
# 进行数据增强
augmented_image = augment_data(image)
# 展示增强后的图像
augmented_image.show()