Sangwon78 commited on
Commit
e253bbe
ยท
verified ยท
1 Parent(s): 7578713

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
app.py CHANGED
@@ -3,58 +3,56 @@ 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()
 
3
  import numpy as np
4
  from PIL import Image, ImageEnhance
5
 
6
+ def process_image(image, filter_type, brightness, tone, sensitivity):
7
+ # Convert image to OpenCV format
8
  img_array = np.array(image)
9
  if filter_type == "Grayscale":
10
+ img_processed = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
11
+ img_processed = cv2.cvtColor(img_processed, cv2.COLOR_GRAY2RGB) # Convert back to 3-channel
12
  elif filter_type == "Color":
13
+ img_processed = img_array # Keep original color
 
 
 
14
 
15
+ # Adjust tone and sensitivity
16
+ img_processed = cv2.convertScaleAbs(img_processed, alpha=tone, beta=sensitivity)
17
+
18
  # Convert to PIL for brightness adjustment
19
+ img_pil = Image.fromarray(img_processed)
20
+ enhancer = ImageEnhance.Brightness(img_pil)
21
+ img_brightened = enhancer.enhance(brightness)
22
+
23
+ return np.array(img_brightened)
24
+
25
+ def save_image(image, output_format):
26
+ output_path = f"output.{output_format.lower()}"
27
+ pil_image = Image.fromarray(image)
28
+ pil_image.save(output_path)
29
+ return output_path
30
+
31
+ def app_function(image, filter_type, brightness, tone, sensitivity, output_format):
32
+ processed_image = process_image(image, filter_type, brightness, tone, sensitivity)
33
+ download_path = save_image(processed_image, output_format)
34
+ return processed_image, download_path
35
+
36
+ with gr.Blocks() as demo:
 
 
 
 
37
  gr.Markdown("### ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ๋ฐ ๋‹ค์šด๋กœ๋“œ ํ”„๋กœ๊ทธ๋žจ")
38
 
39
  with gr.Row():
40
  with gr.Column():
41
+ image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
42
+ filter_type = gr.Radio(["Grayscale", "Color"], label="ํ•„ํ„ฐ ์„ ํƒ", value="Grayscale")
43
  brightness = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="๋ฐ๊ธฐ ์กฐ์ ˆ")
44
  tone = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="ํ†ค ์กฐ์ ˆ")
45
+ sensitivity = gr.Slider(-100, 100, step=10, value=0, label="ํ•„ํ„ฐ ๊ฐ๋„")
46
+ output_format = gr.Radio(["JPG", "PNG", "PDF"], label="์ถœ๋ ฅ ํ˜•์‹ ์„ ํƒ", value="JPG")
47
  with gr.Column():
48
+ output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€")
49
+ download_button = gr.File(label="๋‹ค์šด๋กœ๋“œ")
50
 
51
+ btn = gr.Button("์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ๋ฐ ์ €์žฅ")
52
+ btn.click(
53
+ app_function,
54
+ inputs=[image_input, filter_type, brightness, tone, sensitivity, output_format],
55
+ outputs=[output_image, download_button]
56
  )
57
 
58
+ demo.launch()