Sangwon78 commited on
Commit
57ddb95
ยท
verified ยท
1 Parent(s): ddf0061

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -58
app.py CHANGED
@@ -1,69 +1,60 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- from PIL import Image
5
- import io
6
 
7
- # ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ํ•จ์ˆ˜
8
- def process_image(image, brightness=1.0, contrast=1.0, tone=0.0):
9
- image = np.array(image)
 
 
 
 
 
10
 
11
- # ํ‘๋ฐฑ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
12
- gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
13
- gray_image = cv2.convertScaleAbs(gray_image, alpha=contrast, beta=brightness * 50)
 
 
 
 
 
 
 
 
14
 
15
- # ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
16
- color_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
17
- hsv_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
18
- hsv_image[:, :, 0] = np.clip(hsv_image[:, :, 0] + tone, 0, 179)
19
- color_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
 
 
 
20
 
21
- gray_pil = Image.fromarray(gray_image)
22
- color_pil = Image.fromarray(color_image)
23
- return gray_pil, color_pil
24
 
25
- # ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ํ•จ์ˆ˜
26
- def download_image(image, file_format):
27
- buffer = io.BytesIO()
28
- image.save(buffer, format=file_format.upper())
29
- buffer.seek(0)
30
- return buffer
 
 
 
 
 
 
 
31
 
32
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค
33
- def main():
34
- with gr.Blocks() as app:
35
- gr.Markdown("# ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ๋ฐ ์กฐ์ • ์•ฑ")
36
-
37
- with gr.Row():
38
- input_image = gr.Image(type="pil", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
39
- file_format = gr.Radio(["JPG", "PNG", "PDF"], value="JPG", label="ํŒŒ์ผ ํ˜•์‹ ์„ ํƒ")
40
-
41
- with gr.Row():
42
- brightness = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="๋ฐ๊ธฐ")
43
- contrast = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="๋Œ€๋น„")
44
- tone = gr.Slider(-50, 50, value=0, step=1, label="ํ†ค")
45
-
46
- with gr.Row():
47
- gray_output = gr.Image(label="ํ‘๋ฐฑ ์ด๋ฏธ์ง€")
48
- color_output = gr.Image(label="์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€")
49
-
50
- with gr.Row():
51
- gray_download = gr.File(label="ํ‘๋ฐฑ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ")
52
- color_download = gr.File(label="์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ")
53
-
54
- def update(image, brightness, contrast, tone, file_format):
55
- gray_img, color_img = process_image(image, brightness, contrast, tone)
56
- gray_file = download_image(gray_img, file_format)
57
- color_file = download_image(color_img, file_format)
58
- return gray_img, color_img, gray_file, color_file
59
 
60
- input_image.change(
61
- update,
62
- inputs=[input_image, brightness, contrast, tone, file_format],
63
- outputs=[gray_output, color_output, gray_download, color_download]
64
- )
65
-
66
  app.launch()
67
-
68
- if __name__ == "__main__":
69
- main()
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ from PIL import Image, ImageEnhance
 
5
 
6
+ def process_image(image, filter_type, brightness, tone, sensitivity, output_format):
7
+ # Convert uploaded image to OpenCV format
8
+ img_array = np.array(image)
9
+ if filter_type == "Grayscale":
10
+ processed_img = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
11
+ processed_img = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB) # Grayscale ์œ ์ง€ ํ›„ RGB๋กœ ๋ณ€ํ™˜
12
+ elif filter_type == "Color":
13
+ processed_img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) # OpenCV์˜ ๊ธฐ๋ณธ BGR ์‚ฌ์šฉ
14
 
15
+ # Apply tone and sensitivity filters (basic simulation)
16
+ processed_img = cv2.convertScaleAbs(processed_img, alpha=tone, beta=sensitivity)
17
+
18
+ # Convert to PIL for brightness adjustment
19
+ processed_img = Image.fromarray(processed_img)
20
+ enhancer = ImageEnhance.Brightness(processed_img)
21
+ processed_img = enhancer.enhance(brightness)
22
+
23
+ # Convert final image for display
24
+ final_img = np.array(processed_img)
25
+ return final_img
26
 
27
+ # Gradio Interface
28
+ def download_image(image, output_format):
29
+ if output_format == "JPG":
30
+ return image.save("output.jpg", "JPEG")
31
+ elif output_format == "PNG":
32
+ return image.save("output.png", "PNG")
33
+ elif output_format == "PDF":
34
+ return image.save("output.pdf", "PDF")
35
 
36
+ def app_ui(image, filter_type, brightness, tone, sensitivity, output_format):
37
+ processed_img = process_image(image, filter_type, brightness, tone, sensitivity, output_format)
38
+ return Image.fromarray(processed_img)
39
 
40
+ with gr.Blocks() as app:
41
+ gr.Markdown("### ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ๋ฐ ๋‹ค์šด๋กœ๋“œ ํ”„๋กœ๊ทธ๋žจ")
42
+
43
+ with gr.Row():
44
+ with gr.Column():
45
+ img_input = gr.Image(label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ", type="pil")
46
+ filter_choice = gr.Radio(["Grayscale", "Color"], label="ํ•„ํ„ฐ ์„ ํƒ")
47
+ brightness = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="๋ฐ๊ธฐ ์กฐ์ ˆ")
48
+ tone = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="ํ†ค ์กฐ์ ˆ")
49
+ sensitivity = gr.Slider(0, 100, step=10, value=0, label="ํ•„ํ„ฐ ๊ฐ๋„")
50
+ output_format = gr.Radio(["JPG", "PNG", "PDF"], label="์ถœ๋ ฅ ํ˜•์‹ ์„ ํƒ")
51
+ with gr.Column():
52
+ img_output = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€")
53
 
54
+ img_output = gr.Interface(
55
+ fn=app_ui,
56
+ inputs=[img_input, filter_choice, brightness, tone, sensitivity, output_format],
57
+ outputs=img_output,
58
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
 
 
 
 
 
 
60
  app.launch()