IdaLee commited on
Commit
5d8502c
·
1 Parent(s): b792375

Create pictureDeal.py

Browse files
Files changed (1) hide show
  1. pictureDeal.py +65 -0
pictureDeal.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from PIL import Image, ImageEnhance
3
+ import gradio as gr
4
+ from sklearn.cluster import KMeans
5
+ import numpy as np
6
+
7
+ with gr.Blocks() as interface:
8
+
9
+ with gr.Row():
10
+ n_colors = gr.Slider(2, 32, 12, step=1, label="图片要加工的目标颜色数量")
11
+
12
+ with gr.Row():
13
+ img_input = gr.Image()
14
+ img_output = gr.Image()
15
+
16
+ section_btn1 = gr.Button("合并色彩")
17
+
18
+
19
+ def img_fit_predict(img,n_colors):
20
+ data = img.reshape(-1,3)
21
+ kmeans = KMeans(n_clusters=n_colors)
22
+ y_ = kmeans.fit_predict(data)
23
+ colors = kmeans.cluster_centers_/255
24
+ output_temp = colors[y_].reshape(img.shape)
25
+ return output_temp
26
+
27
+ section_btn1.click(img_fit_predict, inputs=[img_input,n_colors], outputs=img_output)
28
+
29
+ with gr.Row():
30
+ gaussian_blur = gr.Slider(1, 13, 13, step=2, label="整体降噪参数调整")
31
+ structuring_element = gr.Slider(1, 13, 3, step=2, label="去除小噪声")
32
+ canny_start = gr.Slider(1, 200, 4, step=1, label="边缘检测-开始参数")
33
+ canny_end = gr.Slider(1, 200, 10, step=1, label="边缘检测-结束参数")
34
+
35
+ with gr.Row():
36
+ thresh_val = gr.Slider(50, 500, 205, step=1, label="二值图像-thresh")
37
+ maxval = gr.Slider(50, 500, 330, step=1, label="二值图像-maxval")
38
+ enhance = gr.Slider(0, 1, 0.8, step=0.1, label="增强颜色-enhance")
39
+ blend = gr.Slider(0, 1, 0.4, step=0.1, label="增强颜色-blend")
40
+
41
+ section_btn2 = gr.Button("调整图片")
42
+ with gr.Row():
43
+ closed_output = gr.Image()
44
+ img_param_output = gr.Image()
45
+
46
+ def turn_arguments(img,img_output,gaussian_blur,structuring_element,canny_start,canny_end,thresh_val,maxval,enhance,blend):
47
+ gray = cv2.cvtColor(img_output, cv2.COLOR_BGR2GRAY)
48
+ gray = cv2.GaussianBlur(gray, (gaussian_blur,gaussian_blur), 0)
49
+ edges = cv2.Canny(gray, canny_start, canny_end)
50
+ _, thresh = cv2.threshold(edges, thresh_val, maxval, cv2.THRESH_BINARY)
51
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuring_element, structuring_element))
52
+ closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
53
+ image = Image.fromarray(img_output)
54
+ closed = closed.astype(img.dtype)
55
+ enhancer = ImageEnhance.Color(image=image)
56
+ img1 = enhancer.enhance(enhance).convert('RGB')
57
+ img2 = Image.fromarray(closed).convert('RGB')
58
+ union_img = np.asarray(Image.blend(img2, img1, blend))
59
+ return closed,union_img
60
+
61
+ section_btn2.click(turn_arguments,inputs=[img_input, img_output,gaussian_blur,
62
+ structuring_element,canny_start,canny_end,thresh_val,maxval,enhance,blend ],
63
+ outputs = [closed_output,img_param_output])
64
+
65
+ interface.launch(share=True)