Kims12 commited on
Commit
16f9a51
โ€ข
1 Parent(s): b99b09d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -53
app.py CHANGED
@@ -1,76 +1,70 @@
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="ํ•„ํ„ฐ ๊ฐ•๋„")
 
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
+ # ์ฐธ์กฐ ์ฝ”๋“œ์— ๋”ฐ๋ผ PIL์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”ผ๋ถ€ ํ†ค์„ ๊ท ์ผํ•˜๊ฒŒ ํ•˜๊ณ  ์„ ๋ช…๋„๋ฅผ ์กฐ์ ˆ
17
+ from PIL import Image, ImageEnhance
18
+ image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
19
+
20
+ enhancer = ImageEnhance.Sharpness(image_pil)
21
+ image_pil = enhancer.enhance(1 + 0.1 * intensity)
22
+
23
+ enhancer = ImageEnhance.Color(image_pil)
24
+ image_pil = enhancer.enhance(1 + 0.1 * intensity)
25
+
26
+ enhanced_image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
27
+ return enhanced_image
28
+ elif filter_type == "Warm Tone":
29
+ increase_red = np.array([[1.0 + 0.2 * normalized_intensity, 0.0, 0.0],
30
+ [0.0, 1.0, 0.0],
31
+ [0.0, 0.0, 1.0 - 0.2 * normalized_intensity]])
32
+ warm_image = cv2.transform(image, increase_red)
33
+ return warm_image
34
+ elif filter_type == "Cold Tone":
35
+ increase_blue = np.array([[1.0 - 0.2 * normalized_intensity, 0.0, 0.0],
36
+ [0.0, 1.0, 0.0],
37
+ [0.0, 0.0, 1.0 + 0.2 * normalized_intensity]])
38
+ cold_image = cv2.transform(image, increase_blue)
39
+ return cold_image
40
+ elif filter_type == "High-Key":
41
+ high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.2 * normalized_intensity, beta=30)
42
  return high_key
43
+ elif filter_type == "Low-Key":
44
+ low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.3 * normalized_intensity, beta=-30)
 
 
 
 
45
  return low_key
46
+ elif filter_type == "Haze":
47
+ haze = cv2.addWeighted(image, 1.0 - 0.3 * normalized_intensity, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
 
 
 
48
  return haze
49
+ else:
50
+ return image
 
 
 
 
 
 
 
51
 
52
  def convert_to_grayscale(image):
53
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
54
  return gray_image
55
 
56
  def convert_and_save(image, filter_type, intensity):
57
  filtered_image = apply_filter(image, filter_type, intensity)
58
  output_path = "output.jpg"
59
+ cv2.imwrite(output_path, filtered_image)
60
  return filtered_image, output_path
61
 
62
  iface = gr.Interface(
63
  fn=convert_and_save,
64
  inputs=[
65
+ "image",
66
  gr.Radio(
67
+ ["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"],
68
  label="ํ•„ํ„ฐ ์„ ํƒ"
69
  ),
70
  gr.Slider(minimum=1, maximum=100, value=50, label="ํ•„ํ„ฐ ๊ฐ•๋„")