Kims12 commited on
Commit
b99b09d
β€’
1 Parent(s): 8c4ce03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -48
app.py CHANGED
@@ -1,71 +1,76 @@
1
  import gradio as gr
2
- import cv2
3
  import numpy as np
4
 
5
- def apply_filter(image, filter_type, intensity):
6
- # 강도λ₯Ό 0.0μ—μ„œ 1.0 μ‚¬μ΄λ‘œ μ •κ·œν™”
7
- normalized_intensity = intensity / 100.0
 
 
8
 
9
- if filter_type == "Grayscale":
10
- return convert_to_grayscale(image)
11
- elif filter_type == "Soft Glow":
12
- gaussian = cv2.GaussianBlur(image, (15, 15), 0)
13
- soft_glow = cv2.addWeighted(image, 1 - normalized_intensity, gaussian, normalized_intensity, 0)
14
- return soft_glow
15
- elif filter_type == "Portrait Enhancer":
16
- # Step 1: Detail Enhance 적용 (λ‹€μ†Œ κ°•ν•˜κ²Œ)
17
- enhanced = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.1 + 0.1 * normalized_intensity)
18
-
19
- # Step 2: Gaussian Blur 적용 (강도 μ‘°μ •)
20
- blurred = cv2.GaussianBlur(enhanced, (5, 5), 0)
21
-
22
- # Step 3: Bilateral Filter 적용 (μ•½ν•˜κ²Œ, 경계 보쑴)
23
- smoothed = cv2.bilateralFilter(blurred, d=5, sigmaColor=50, sigmaSpace=50)
24
-
25
- # Step 4: 원본 이미지와 ν˜Όν•©ν•˜μ—¬ μ„ λͺ…함 μœ μ§€
26
- final_image = cv2.addWeighted(enhanced, 0.7, smoothed, 0.3, 0)
27
-
28
- return final_image
29
- elif filter_type == "Warm Tone":
30
- increase_red = np.array([[1.0 + 0.2 * normalized_intensity, 0.0, 0.0],
31
- [0.0, 1.0, 0.0],
32
- [0.0, 0.0, 1.0 - 0.2 * normalized_intensity]])
33
- warm_image = cv2.transform(image, increase_red)
34
- return warm_image
35
- elif filter_type == "Cold Tone":
36
- increase_blue = np.array([[1.0 - 0.2 * normalized_intensity, 0.0, 0.0],
37
- [0.0, 1.0, 0.0],
38
- [0.0, 0.0, 1.0 + 0.2 * normalized_intensity]])
39
- cold_image = cv2.transform(image, increase_blue)
40
- return cold_image
41
- elif filter_type == "High-Key":
42
- high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.2 * normalized_intensity, beta=30)
43
  return high_key
44
- elif filter_type == "Low-Key":
45
- low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.3 * normalized_intensity, beta=-30)
 
 
 
 
46
  return low_key
47
- elif filter_type == "Haze":
48
- haze = cv2.addWeighted(image, 1.0 - 0.3 * normalized_intensity, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
 
 
 
49
  return haze
50
- else:
51
- return image
 
 
 
 
 
 
 
52
 
53
  def convert_to_grayscale(image):
54
- gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
55
  return gray_image
56
 
57
  def convert_and_save(image, filter_type, intensity):
58
  filtered_image = apply_filter(image, filter_type, intensity)
59
  output_path = "output.jpg"
60
- cv2.imwrite(output_path, filtered_image)
61
  return filtered_image, output_path
62
 
63
  iface = gr.Interface(
64
  fn=convert_and_save,
65
  inputs=[
66
- "image",
67
  gr.Radio(
68
- ["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"],
69
  label="ν•„ν„° 선택"
70
  ),
71
  gr.Slider(minimum=1, maximum=100, value=50, label="ν•„ν„° 강도")
 
1
  import gradio as gr
2
+ from PIL import Image, ImageEnhance, ImageOps, ImageFilter
3
  import numpy as np
4
 
5
+ def apply_filter(image, filter_name, intensity):
6
+ if filter_name == "Soft Glow":
7
+ glow = image.filter(ImageFilter.GaussianBlur(intensity))
8
+ enhanced_image = Image.blend(image, glow, 0.5)
9
+ return enhanced_image
10
 
11
+ elif filter_name == "Portrait Enhancer":
12
+ enhancer = ImageEnhance.Sharpness(image)
13
+ image = enhancer.enhance(1 + 0.1 * intensity)
14
+ enhancer = ImageEnhance.Color(image)
15
+ image = enhancer.enhance(1 + 0.1 * intensity)
16
+ return image
17
+
18
+ elif filter_name == "Warm Tone":
19
+ warm_tone = ImageEnhance.Color(image).enhance(1 + 0.1 * intensity)
20
+ return warm_tone
21
+
22
+ elif filter_name == "Cold Tone":
23
+ blue_overlay = Image.new('RGB', image.size, (173, 216, 230))
24
+ blended_image = Image.blend(image, blue_overlay, 0.05 * intensity)
25
+ enhancer = ImageEnhance.Sharpness(blended_image)
26
+ sharpened_image = enhancer.enhance(1 + 0.3 * intensity)
27
+ return sharpened_image
28
+
29
+ elif filter_name == "High-Key":
30
+ enhancer = ImageEnhance.Brightness(image)
31
+ high_key = enhancer.enhance(1 + 0.1 * intensity)
32
+ enhancer = ImageEnhance.Contrast(high_key)
33
+ high_key = enhancer.enhance(0.8 + 0.02 * intensity)
 
 
 
 
 
 
 
 
 
 
 
34
  return high_key
35
+
36
+ elif filter_name == "Low-Key":
37
+ enhancer = ImageEnhance.Contrast(image)
38
+ low_key = enhancer.enhance(1 + 0.1 * intensity)
39
+ enhancer = ImageEnhance.Brightness(low_key)
40
+ low_key = enhancer.enhance(1 - 0.05 * intensity)
41
  return low_key
42
+
43
+ elif filter_name == "Haze":
44
+ haze = image.filter(ImageFilter.GaussianBlur(2 * intensity))
45
+ enhancer = ImageEnhance.Brightness(haze)
46
+ haze = enhancer.enhance(1 + 0.05 * intensity)
47
  return haze
48
+
49
+ elif filter_name == "Monochrome":
50
+ mono = image.convert("L")
51
+ mono = ImageOps.colorize(mono, black="black", white="white")
52
+ enhancer = ImageEnhance.Contrast(mono)
53
+ mono = enhancer.enhance(1 + 0.1 * intensity)
54
+ return mono
55
+
56
+ return image
57
 
58
  def convert_to_grayscale(image):
59
+ gray_image = ImageOps.grayscale(image)
60
  return gray_image
61
 
62
  def convert_and_save(image, filter_type, intensity):
63
  filtered_image = apply_filter(image, filter_type, intensity)
64
  output_path = "output.jpg"
65
+ filtered_image.save(output_path)
66
  return filtered_image, output_path
67
 
68
  iface = gr.Interface(
69
  fn=convert_and_save,
70
  inputs=[
71
+ gr.Image(type="pil", label="이미지 μ—…λ‘œλ“œ"),
72
  gr.Radio(
73
+ ["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
74
  label="ν•„ν„° 선택"
75
  ),
76
  gr.Slider(minimum=1, maximum=100, value=50, label="ν•„ν„° 강도")