shashank1024 commited on
Commit
40df648
·
verified ·
1 Parent(s): a128706

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -23
app.py CHANGED
@@ -1,50 +1,40 @@
1
- import cv2
2
- import numpy as np
3
  import gradio as gr
4
 
5
  def apply_blur(image):
6
- return cv2.GaussianBlur(image, (15, 15), 0)
7
 
8
  def apply_invert(image):
9
- return cv2.bitwise_not(image)
10
 
11
  def apply_sharpen(image):
12
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
13
- return cv2.filter2D(image, -1, kernel)
14
 
15
  def apply_black_white(image):
16
- return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
17
 
18
  def apply_filter(image, filter_type):
19
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
20
-
21
  if filter_type == 'blur':
22
- result = apply_blur(image)
23
  elif filter_type == 'invert':
24
- result = apply_invert(image)
25
  elif filter_type == 'sharpen':
26
- result = apply_sharpen(image)
27
  elif filter_type == 'black & white':
28
- result = apply_black_white(image)
29
  else:
30
- result = image
31
-
32
- if len(result.shape) == 2:
33
- result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
34
-
35
- return cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
36
 
37
  def run_interface():
38
  filter_choices = ['blur', 'invert', 'sharpen', 'black & white']
39
 
40
  interface = gr.Interface(
41
  fn=apply_filter,
42
- inputs=[gr.Image(type='numpy'),
43
  gr.Dropdown(filter_choices, label='Select Filter')],
44
- outputs=gr.Image(type='numpy'),
45
- live=2
46
  )
47
  return interface
48
 
49
  iface = run_interface()
50
- iface.launch(share=true)
 
1
+ from PIL import Image, ImageFilter, ImageOps
 
2
  import gradio as gr
3
 
4
  def apply_blur(image):
5
+ return image.filter(ImageFilter.BLUR)
6
 
7
  def apply_invert(image):
8
+ return ImageOps.invert(image.convert("RGB"))
9
 
10
  def apply_sharpen(image):
11
+ return image.filter(ImageFilter.SHARPEN)
 
12
 
13
  def apply_black_white(image):
14
+ return image.convert("L") # Convert to grayscale (black & white)
15
 
16
  def apply_filter(image, filter_type):
 
 
17
  if filter_type == 'blur':
18
+ return apply_blur(image)
19
  elif filter_type == 'invert':
20
+ return apply_invert(image)
21
  elif filter_type == 'sharpen':
22
+ return apply_sharpen(image)
23
  elif filter_type == 'black & white':
24
+ return apply_black_white(image)
25
  else:
26
+ return image
 
 
 
 
 
27
 
28
  def run_interface():
29
  filter_choices = ['blur', 'invert', 'sharpen', 'black & white']
30
 
31
  interface = gr.Interface(
32
  fn=apply_filter,
33
+ inputs=[gr.Image(type='pil'),
34
  gr.Dropdown(filter_choices, label='Select Filter')],
35
+ outputs=gr.Image(type='pil')
 
36
  )
37
  return interface
38
 
39
  iface = run_interface()
40
+ iface.launch()