NhatTan commited on
Commit
a341af5
·
1 Parent(s): 078fdec

17/02/2023 v1

Browse files
Image_Filters_Streamlit_app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Checkout this tutorial
2
+ # https://blog.loginradius.com/engineering/guest-post/opencv-web-app-with-streamlit/
3
+ # Online deployment:
4
+ # https://towardsdatascience.com/3-easy-ways-to-deploy-your-streamlit-web-app-online-7c88bb1024b1
5
+ # https://www.youtube.com/watch?v=4SO3CUWPYf0
6
+
7
+ # Run: streamlit run Image_Filters_Streamlit_app.py
8
+
9
+ import io
10
+ import base64
11
+ import cv2
12
+ from PIL import Image
13
+ from filters import *
14
+
15
+ # Generating a link to download a particular image file.
16
+ def get_image_download_link(img, filename, text):
17
+ buffered = io.BytesIO()
18
+ img.save(buffered, format = 'JPEG')
19
+ img_str = base64.b64encode(buffered.getvalue()).decode()
20
+ href = f'<a href="data:file/txt;base64,{img_str}" download="{filename}">{text}</a>'
21
+ return href
22
+
23
+ # Set title.
24
+ st.title('Artistic Image Filters')
25
+
26
+ # Upload image.
27
+ uploaded_file = st.file_uploader('Choose an image file:', type=['png','jpg'])
28
+
29
+ if uploaded_file is not None:
30
+ # Convert the file to an opencv image.
31
+ raw_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
32
+ img = cv2.imdecode(raw_bytes, cv2.IMREAD_COLOR)
33
+ input_col, output_col = st.columns(2)
34
+ with input_col:
35
+ st.header('Original')
36
+ # Display uploaded image.
37
+ st.image(img, channels='BGR', use_column_width=True)
38
+
39
+ st.header('Filter Examples:')
40
+ # Display a selection box for choosing the filter to apply.
41
+ option = st.selectbox('Select a filter:',
42
+ ( 'None',
43
+ 'Black and White',
44
+ 'Sepia / Vintage',
45
+ 'Vignette Effect',
46
+ 'Pencil Sketch',
47
+ ))
48
+
49
+ # Define columns for thumbnail images.
50
+ col1, col2, col3, col4 = st.columns(4)
51
+ with col1:
52
+ st.caption('Black and White')
53
+ st.image('filter_bw.jpg')
54
+ with col2:
55
+ st.caption('Sepia / Vintage')
56
+ st.image('filter_sepia.jpg')
57
+ with col3:
58
+ st.caption('Vignette Effect')
59
+ st.image('filter_vignette.jpg')
60
+ with col4:
61
+ st.caption('Pencil Sketch')
62
+ st.image('filter_pencil_sketch.jpg')
63
+
64
+ # Flag for showing output image.
65
+ output_flag = 1
66
+ # Colorspace of output image.
67
+ color = 'BGR'
68
+
69
+ # Generate filtered image based on the selected option.
70
+ if option == 'None':
71
+ # Don't show output image.
72
+ output_flag = 0
73
+ elif option == 'Black and White':
74
+ output = bw_filter(img)
75
+ color = 'GRAY'
76
+ elif option == 'Sepia / Vintage':
77
+ output = sepia(img)
78
+ elif option == 'Vignette Effect':
79
+ level = st.slider('level', 1, 5, 2)
80
+ output = vignette(img, level)
81
+ elif option == 'Pencil Sketch':
82
+ ksize = st.slider('Blur kernel size', 1, 11, 5, step=2)
83
+ output = pencil_sketch(img, ksize)
84
+ color = 'GRAY'
85
+
86
+ with output_col:
87
+ if output_flag == 1:
88
+ st.header('Output')
89
+ st.image(output, channels=color)
90
+ # fromarray convert cv2 image into PIL format for saving it using download link.
91
+ if color == 'BGR':
92
+ result = Image.fromarray(output[:,:,::-1])
93
+ else:
94
+ result = Image.fromarray(output)
95
+ # Display link.
96
+ st.markdown(get_image_download_link(result,'output.png','Download '+'Output'),
97
+ unsafe_allow_html=True)
98
+
__pycache__/filters.cpython-310.pyc ADDED
Binary file (1.54 kB). View file
 
filter_bw.jpg ADDED
filter_pencil_sketch.jpg ADDED
filter_sepia.jpg ADDED
filter_vignette.jpg ADDED
filters.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import streamlit as st
4
+
5
+
6
+ # Refer to the application notebook implement the following filters
7
+
8
+ @st.cache_data
9
+ def bw_filter(img):
10
+ # Write your code here to convert img to a gray image
11
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
12
+ return img_gray
13
+
14
+ @st.cache_data
15
+ def vignette(img, level=2):
16
+ # Write your code here to create the vignette effect
17
+ height, width = img.shape[:2]
18
+
19
+ # Generate vignette mask using Gaussian kernels.
20
+ X_resultant_kernel = cv2.getGaussianKernel(width, width/level)
21
+ Y_resultant_kernel = cv2.getGaussianKernel(height, height/level)
22
+
23
+ # Generating resultant_kernel matrix.
24
+ # H x 1 * 1 x W
25
+ kernel = Y_resultant_kernel * X_resultant_kernel.T
26
+ mask = kernel / kernel.max()
27
+
28
+ img_vignette = np.copy(img)
29
+
30
+ # Applying the mask to each channel in the input image.
31
+ for i in range(3):
32
+ img_vignette[:,:,i] = img_vignette[:,:,i] * mask
33
+
34
+
35
+ return img_vignette
36
+
37
+ @st.cache_data
38
+ def sepia(img):
39
+ # Write your code here to create the sepia effect
40
+ img_sepia = img.copy()
41
+ # Converting to RGB as sepia matrix below is for RGB.
42
+ img_sepia = cv2.cvtColor(img_sepia, cv2.COLOR_BGR2RGB)
43
+ img_sepia = np.array(img_sepia, dtype = np.float64)
44
+ img_sepia = cv2.transform(img_sepia, np.matrix([[0.393, 0.769, 0.189],
45
+ [0.349, 0.686, 0.168],
46
+ [0.272, 0.534, 0.131]]))
47
+ # Clip values to the range [0, 255].
48
+ img_sepia = np.clip(img_sepia, 0, 255)
49
+ img_sepia = np.array(img_sepia, dtype = np.uint8)
50
+ img_sepia = cv2.cvtColor(img_sepia, cv2.COLOR_RGB2BGR)
51
+ return img_sepia
52
+
53
+ @st.cache_data
54
+ def pencil_sketch(img, ksize=5):
55
+ # Write your code here to create the pencil sketch effect
56
+ img_blur = cv2.GaussianBlur(img, (ksize, ksize), 0, 0)
57
+ img_sketch, _ = cv2.pencilSketch(img_blur)
58
+ return img_sketch
59
+
60
+ # Don't be constrained, add your own filters here
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy==1.24.2
2
+ opencv_python==4.7.0.68
3
+ Pillow==9.4.0
4
+ streamlit==1.18.1