joyjonesmark commited on
Commit
c3a39d7
Β·
1 Parent(s): 52b6f3f

app as updated

Browse files
Files changed (1) hide show
  1. app.py +113 -67
app.py CHANGED
@@ -2,86 +2,132 @@ import streamlit as st
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
- # Function to apply filters
7
- def apply_filter(image, filter_name, scale):
8
- if filter_name == "Grayscale":
9
- return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10
- elif filter_name == "Blur":
11
- # Ensure kernel size is a positive odd integer
12
- ksize = max(1, scale)
13
- ksize = ksize if ksize % 2 == 1 else ksize + 1
14
- return cv2.GaussianBlur(image, (ksize, ksize), 0)
15
- elif filter_name == "Sharpen":
16
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
17
- return cv2.filter2D(image, -1, kernel)
18
- elif filter_name == "Edge Detection":
19
- # Ensure scale is within valid range for Canny
20
- scale = max(1, scale)
21
- return cv2.Canny(image, scale, scale * 2)
22
- elif filter_name == "Brightness":
23
- hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
24
- hsv[:, :, 2] = np.clip(hsv[:, :, 2] + scale, 0, 255)
25
- return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
26
- elif filter_name == "Contrast":
27
- alpha = scale / 50.0
28
- return cv2.convertScaleAbs(image, alpha=alpha, beta=0)
29
- elif filter_name == "Threshold":
30
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
31
- _, thresh = cv2.threshold(gray, scale, 255, cv2.THRESH_BINARY)
32
- return thresh
33
- elif filter_name == "Sepia":
34
- kernel = np.array([[0.272, 0.534, 0.131],
35
- [0.349, 0.686, 0.168],
36
- [0.393, 0.769, 0.189]])
37
- return cv2.transform(image, kernel)
38
- else:
39
- return image
40
-
41
- # Streamlit app
42
- st.title("🎨 Image Processing App πŸ–ΌοΈ")
43
- st.write("Upload an image and apply filters to see the magic! ✨")
44
 
45
  # Upload image
46
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
47
 
48
  if uploaded_file is not None:
49
- # Read and display the original image
50
- image = Image.open(uploaded_file)
51
- image = np.array(image)
52
- st.write("### Original Image πŸ–ΌοΈ")
53
- st.image(image, caption="Original Image", use_container_width=True)
54
-
55
- # Select filter
56
- filter_name = st.selectbox(
57
- "Choose a filter πŸŽ›οΈ",
58
- ["Grayscale", "Blur", "Sharpen", "Edge Detection", "Brightness", "Contrast", "Threshold", "Sepia"]
59
- )
60
-
61
- # Add slider for filter scale
62
- scale = st.slider(f"Adjust {filter_name} intensity βš™οΈ", 1, 100, 50)
63
 
64
- # Apply filter
65
- processed_image = apply_filter(image, filter_name, scale)
66
-
67
- # Display input and output images in the same row
68
  col1, col2 = st.columns(2)
69
  with col1:
70
- st.write("### Original Image πŸ–ΌοΈ")
71
- st.image(image, caption="Original Image", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  with col2:
73
- st.write("### Processed Image 🎨")
74
- st.image(processed_image, caption=f"{filter_name} Applied", use_container_width=True)
75
 
76
  # Download button for processed image
77
- processed_image_pil = Image.fromarray(processed_image)
 
 
 
 
 
78
  st.download_button(
79
- label="Download Processed Image ⬇️",
80
- data=processed_image_pil.tobytes(),
81
  file_name="processed_image.png",
82
- mime="image/png"
83
  )
84
 
85
- st.success("βœ… Image processing complete! Enjoy your masterpiece! 🎨")
86
  else:
87
- st.info("πŸ‘† Please upload an image to get started! πŸ–ΌοΈ")
 
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
5
+ import io
6
 
7
+ # App title and emojis
8
+ st.title("πŸ“Έ Image Processing App 🎨")
9
+ st.write("Upload an image, apply filters, and download the processed image! ✨")
10
+
11
+ # Sidebar for feature selection
12
+ st.sidebar.header("🎨 Filter Options")
13
+ filter_type = st.sidebar.selectbox(
14
+ "Select a filter:",
15
+ [
16
+ "Blur",
17
+ "Edge Detection (Canny)",
18
+ "Grayscale",
19
+ "Sharpen",
20
+ "Sepia",
21
+ "Invert Colors",
22
+ "Brightness Adjustment",
23
+ "Contrast Adjustment",
24
+ ],
25
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Upload image
28
+ uploaded_file = st.file_uploader(".Upload an image πŸ–ΌοΈ", type=["jpg", "jpeg", "png"])
29
 
30
  if uploaded_file is not None:
31
+ # Convert the file to an OpenCV image
32
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
33
+ img = cv2.imdecode(file_bytes, 1)
34
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Display original image
 
 
 
37
  col1, col2 = st.columns(2)
38
  with col1:
39
+ st.subheader("Original Image πŸ“·")
40
+ st.image(img, use_container_width=True)
41
+
42
+ # Apply filters based on user selection
43
+ processed_img = img.copy()
44
+
45
+ if filter_type == "Blur":
46
+ blur_type = st.sidebar.selectbox(
47
+ "Blur Type", ["Gaussian Blur", "Average Blur", "Median Blur", "Bilateral Filter"]
48
+ )
49
+ if blur_type == "Gaussian Blur":
50
+ kernel_size = st.sidebar.slider("Kernel Size πŸ”", 1, 31, 5, step=2)
51
+ sigma_x = st.sidebar.slider("Sigma X (Standard Deviation)", 0, 10, 0)
52
+ processed_img = cv2.GaussianBlur(processed_img, (kernel_size, kernel_size), sigma_x)
53
+
54
+ elif blur_type == "Average Blur":
55
+ kernel_size = st.sidebar.slider("Kernel Size πŸ”", 1, 31, 5, step=2)
56
+ processed_img = cv2.blur(processed_img, (kernel_size, kernel_size))
57
+
58
+ elif blur_type == "Median Blur":
59
+ kernel_size = st.sidebar.slider("Kernel Size πŸ”", 1, 31, 5, step=2)
60
+ processed_img = cv2.medianBlur(processed_img, kernel_size)
61
+
62
+ elif blur_type == "Bilateral Filter":
63
+ diameter = st.sidebar.slider("Diameter", 1, 31, 9, step=2)
64
+ sigma_color = st.sidebar.slider("Sigma Color", 1, 200, 75)
65
+ sigma_space = st.sidebar.slider("Sigma Space", 1, 200, 75)
66
+ processed_img = cv2.bilateralFilter(processed_img, diameter, sigma_color, sigma_space)
67
+
68
+ elif filter_type == "Edge Detection (Canny)":
69
+ lower_threshold = st.sidebar.slider("Lower Threshold ⬇️", 0, 255, 100)
70
+ upper_threshold = st.sidebar.slider("Upper Threshold ⬆️", 0, 255, 200)
71
+ processed_img = cv2.Canny(processed_img, lower_threshold, upper_threshold)
72
+
73
+ elif filter_type == "Grayscale":
74
+ grayscale_method = st.sidebar.selectbox(
75
+ "Grayscale Method", ["Weighted Average", "Simple Average"]
76
+ )
77
+ if grayscale_method == "Weighted Average":
78
+ processed_img = cv2.cvtColor(processed_img, cv2.COLOR_RGB2GRAY)
79
+ else:
80
+ processed_img = np.mean(processed_img, axis=2).astype(np.uint8)
81
+
82
+ elif filter_type == "Sharpen":
83
+ sharpness = st.sidebar.slider("Sharpness Intensity", 1.0, 10.0, 1.0)
84
+ kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) * sharpness
85
+ processed_img = cv2.filter2D(processed_img, -1, kernel)
86
+
87
+ elif filter_type == "Sepia":
88
+ sepia_intensity = st.sidebar.slider("Sepia Intensity", 0.1, 1.0, 1.0)
89
+ sepia_filter = (
90
+ np.array(
91
+ [[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]
92
+ )
93
+ * sepia_intensity
94
+ )
95
+ processed_img = cv2.transform(processed_img, sepia_filter)
96
+ processed_img = np.clip(processed_img, 0, 255)
97
+
98
+ elif filter_type == "Invert Colors":
99
+ blend_factor = st.sidebar.slider("Inversion Blend Factor", 0.0, 1.0, 1.0)
100
+ inverted_img = 255 - processed_img
101
+ processed_img = cv2.addWeighted(
102
+ processed_img, 1 - blend_factor, inverted_img, blend_factor, 0
103
+ )
104
+
105
+ elif filter_type == "Brightness Adjustment":
106
+ brightness = st.sidebar.slider("Brightness β˜€οΈ", -100, 100, 0)
107
+ processed_img = cv2.convertScaleAbs(processed_img, alpha=1, beta=brightness)
108
+
109
+ elif filter_type == "Contrast Adjustment":
110
+ contrast = st.sidebar.slider("Contrast ⚑", 0.1, 3.0, 1.0, step=0.1)
111
+ processed_img = cv2.convertScaleAbs(processed_img, alpha=contrast, beta=0)
112
+
113
+ # Display processed image
114
  with col2:
115
+ st.subheader("Processed Image 🎯")
116
+ st.image(processed_img, use_container_width=True)
117
 
118
  # Download button for processed image
119
+ st.markdown("---")
120
+ st.subheader("Download Processed Image πŸ’Ύ")
121
+ buf = io.BytesIO()
122
+ pil_img = Image.fromarray(processed_img)
123
+ pil_img.save(buf, format="PNG")
124
+ byte_im = buf.getvalue()
125
  st.download_button(
126
+ label="πŸ“₯ Download Image",
127
+ data=byte_im,
128
  file_name="processed_image.png",
129
+ mime="image/png",
130
  )
131
 
 
132
  else:
133
+ st.info("Please upload an image to get started. πŸ“‚")