0xSynapse commited on
Commit
f50d9e0
1 Parent(s): 959ee16

Upload 7 files

Browse files
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+ from filters import *
6
+ import streamlit as st
7
+
8
+
9
+ # Generating a button to download the image file.
10
+ def download_image_button(img, filename, text):
11
+ buffered = io.BytesIO()
12
+ img.save(buffered, format="JPEG")
13
+ img_bytes = buffered.getvalue()
14
+
15
+ # Using st.download_button to handle the download
16
+ st.download_button(label=text, data=img_bytes, file_name=filename, mime="image/jpeg", use_container_width=True)
17
+
18
+
19
+ # Set title.
20
+ st.title("Artistic Image Filters")
21
+
22
+ # Upload image.
23
+ uploaded_file = st.file_uploader("Choose an image file:", type=["png", "jpg"])
24
+
25
+ if uploaded_file is not None:
26
+ # Convert the file to an opencv image.
27
+ raw_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
28
+ img = cv2.imdecode(raw_bytes, cv2.IMREAD_COLOR)
29
+ input_col, output_col = st.columns(2)
30
+ with input_col:
31
+ st.header("Original")
32
+ # Display uploaded image.
33
+ st.image(img, channels="BGR", use_column_width=True)
34
+
35
+ st.header("Filter Examples:")
36
+
37
+ # Display radio buttons for choosing the filter to apply.
38
+ option = st.radio(
39
+ "Select a filter:",
40
+ (
41
+ "None",
42
+ "Black and White",
43
+ "Sepia / Vintage",
44
+ "Vignette Effect",
45
+ "Pencil Sketch",
46
+ ),
47
+ horizontal=True,
48
+ )
49
+
50
+ # Define columns for thumbnail images.
51
+ col1, col2, col3, col4 = st.columns(4)
52
+ with col1:
53
+ st.caption("Black and White")
54
+ st.image("filter_bw.jpg")
55
+ with col2:
56
+ st.caption("Sepia / Vintage")
57
+ st.image("filter_sepia.jpg")
58
+ with col3:
59
+ st.caption("Vignette Effect")
60
+ st.image("filter_vignette.jpg")
61
+ with col4:
62
+ st.caption("Pencil Sketch")
63
+ st.image("filter_pencil_sketch.jpg")
64
+
65
+ # Flag for showing output image.
66
+ output_flag = 1
67
+ # Colorspace of output image.
68
+ color = "BGR"
69
+
70
+ # Generate filtered image based on the selected option.
71
+ if option == "None":
72
+ # Don't show output image.
73
+ output_flag = 0
74
+ elif option == "Black and White":
75
+ output = bw_filter(img)
76
+ color = "GRAY"
77
+ elif option == "Sepia / Vintage":
78
+ output = sepia(img)
79
+ elif option == "Vignette Effect":
80
+ level = st.slider("level", 0, 5, 2)
81
+ output = vignette(img, level)
82
+ elif option == "Pencil Sketch":
83
+ ksize = st.slider("Blur kernel size", 1, 11, 5, step=2)
84
+ output = pencil_sketch(img, ksize)
85
+ color = "GRAY"
86
+
87
+ with output_col:
88
+ if output_flag == 1:
89
+ st.header("Output")
90
+ st.image(output, channels=color)
91
+ # fromarray converts cv2 image into PIL format for saving it using download button.
92
+ if color == "BGR":
93
+ result = Image.fromarray(output[:, :, ::-1])
94
+ else:
95
+ result = Image.fromarray(output)
96
+
97
+ # Display the download button with the text "Download Output"
98
+ download_image_button(result, "output.jpg", "Download Output")
99
+ else:
100
+ st.header("Output")
101
+ st.image(img, channels=color)
102
+
filters.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import streamlit as st
4
+
5
+
6
+ @st.cache_data
7
+ def bw_filter(img):
8
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
9
+ return img_gray
10
+
11
+
12
+ @st.cache_data
13
+ def vignette(img, level=2):
14
+ height, width = img.shape[:2]
15
+
16
+ # Generate vignette mask using Gaussian kernels.
17
+ X_resultant_kernel = cv2.getGaussianKernel(width, width / level)
18
+ Y_resultant_kernel = cv2.getGaussianKernel(height, height / level)
19
+
20
+ # Generating resultant_kernel matrix.
21
+ kernel = Y_resultant_kernel * X_resultant_kernel.T
22
+ mask = kernel / kernel.max()
23
+
24
+ img_vignette = np.copy(img)
25
+
26
+ # Apply the mask to each channel in the input image.
27
+ for i in range(3):
28
+ img_vignette[:, :, i] = img_vignette[:, :, i] * mask
29
+
30
+ return img_vignette
31
+
32
+
33
+ @st.cache_data
34
+ def sepia(img):
35
+ img_sepia = img.copy()
36
+ # Converting to RGB as sepia matrix below is for RGB.
37
+ img_sepia = cv2.cvtColor(img_sepia, cv2.COLOR_BGR2RGB)
38
+ img_sepia = np.array(img_sepia, dtype=np.float64)
39
+ img_sepia = cv2.transform(
40
+ img_sepia, np.matrix([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]])
41
+ )
42
+ # Clip values to the range [0, 255].
43
+ img_sepia = np.clip(img_sepia, 0, 255)
44
+ img_sepia = np.array(img_sepia, dtype=np.uint8)
45
+ img_sepia = cv2.cvtColor(img_sepia, cv2.COLOR_RGB2BGR)
46
+ return img_sepia
47
+
48
+
49
+ @st.cache_data
50
+ def pencil_sketch(img, ksize=5):
51
+ img_blur = cv2.GaussianBlur(img, (ksize, ksize), 0, 0)
52
+ img_sketch, _ = cv2.pencilSketch(img_blur)
53
+ return img_sketch
filters/filter_bw.jpg ADDED
filters/filter_pencil_sketch.jpg ADDED
filters/filter_sepia.jpg ADDED
filters/filter_vignette.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ opencv-python==4.10.0.84
2
+ streamlit==1.38.0