-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduceTestImage.py
111 lines (74 loc) · 3.04 KB
/
ProduceTestImage.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# This file is used to produce images for algorithms testing.
# Reference:
# [Image Processing in OpenCV].(https://docs.opencv.org/4.x/d2/d96/tutorial_py_table_of_contents_imgproc.html).
# [rotate].(https://github.com/PyImageSearch/imutils/blob/master/imutils/convenience.py).
# [contrast, light, color enhance,sharpen].(https://github.com/JiangChenrui/matching_learning_note/blob/master/image_enhance.py).
import cv2 as cv
import numpy as np
from PIL import Image
import os.path as path
from PIL import ImageEnhance
def rotate(image):
def rotate_bound(image, angle):
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
M = cv.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
return cv.warpAffine(image, M, (nW, nH))
return rotate_bound(image, 45)
def enhance_color(image):
enh_col = ImageEnhance.Color(image)
color = 1.5
return enh_col.enhance(color)
def blur(image):
return cv.blur(image, (15, 1))
def sharp(image):
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
return cv.filter2D(image, -1, kernel=kernel)
def contrast(image):
def contrast_brightness_image(src1, a, g):
h, w, ch = src1.shape
src2 = np.zeros([h, w, ch], src1.dtype)
return cv.addWeighted(src1, a, src2, 1 - a, g)
return contrast_brightness_image(image, 1.2, 1)
def resize(image):
return cv.resize(image, (0, 0), fx=1.25, fy=1)
def light(image):
return np.uint8(np.clip((1.3 * image + 10), 0, 255))
def save_img(image, img_name, output_path=None):
cv.imwrite(path.join(output_path, img_name), image, [int(cv.IMWRITE_JPEG_QUALITY), 70])
pass
def show_img(image):
cv.imshow('image', image)
cv.waitKey(0)
pass
def main():
data_img_name = 'path{image.jpg}'
output_path = "path{output}"
data_path = path.join(output_path, data_img_name)
img = cv.imread(data_path)
img_light = light(img)
img_resize = resize(img)
img_contrast = contrast(img)
img_sharp = sharp(img)
img_blur = blur(img)
img_color = enhance_color(Image.open(data_path))
img_rotate = rotate(img)
img_rotate1 = Image.open(data_path).rotate(45)
save_img(img_light, "%s_light.jpg" % data_img_name.split(".")[0], output_path)
save_img(img_resize, "%s_resize.jpg" % data_img_name.split(".")[0], output_path)
save_img(img_contrast, "%s_contrast.jpg" % data_img_name.split(".")[0], output_path)
save_img(img_sharp, "%s_sharp.jpg" % data_img_name.split(".")[0], output_path)
save_img(img_blur, "%s_blur.jpg" % data_img_name.split(".")[0], output_path)
img_color.save(path.join(output_path, "%s_color.jpg" % data_img_name.split(".")[0]))
img_rotate1.save(path.join(output_path, "%s_rotate.jpg" % data_img_name.split(".")[0]))
print("Done")
#show_img(img_rotate)
pass
if __name__ == '__main__':
main()