HFUsman commited on
Commit
95d114b
·
verified ·
1 Parent(s): 04cc5c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -198
app.py CHANGED
@@ -1,228 +1,131 @@
1
  import streamlit as st
2
  from PIL import Image, ImageEnhance, ImageFilter, ImageOps
3
  import numpy as np
4
- import cv2
5
- import io
6
 
7
  # Function to apply grayscale filter
8
  def apply_grayscale(image):
9
  return image.convert("L")
10
 
11
- # Function to adjust brightness
12
- def adjust_brightness(image, factor):
13
  enhancer = ImageEnhance.Brightness(image)
14
  return enhancer.enhance(factor)
15
 
16
- # Function to adjust contrast
17
- def adjust_contrast(image, factor):
18
  enhancer = ImageEnhance.Contrast(image)
19
  return enhancer.enhance(factor)
20
 
21
- # Function to adjust saturation
22
- def adjust_saturation(image, factor):
23
- enhancer = ImageEnhance.Color(image)
24
- return enhancer.enhance(factor)
25
-
26
- # Function to apply sharpness enhancement
27
  def apply_sharpness(image, factor):
28
  enhancer = ImageEnhance.Sharpness(image)
29
  return enhancer.enhance(factor)
30
 
31
- # Function to apply sepia effect
32
- def apply_sepia(image):
33
- img = np.array(image)
34
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
35
- sepia_filter = np.array([[0.393, 0.769, 0.189],
36
- [0.349, 0.686, 0.168],
37
- [0.272, 0.534, 0.131]])
38
- img = cv2.transform(img, sepia_filter)
39
- img = np.clip(img, 0, 255)
40
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
41
- return Image.fromarray(img)
42
-
43
- # Function to apply vignette effect
44
- def apply_vignette(image):
45
- img = np.array(image)
46
- rows, cols = img.shape[:2]
47
- X_resultant_kernel = cv2.getGaussianKernel(cols, cols / 3)
48
- Y_resultant_kernel = cv2.getGaussianKernel(rows, rows / 3)
49
- resultant_kernel = Y_resultant_kernel * X_resultant_kernel.T
50
- mask = 255 * resultant_kernel / np.linalg.norm(resultant_kernel)
51
- img = np.multiply(img, mask[..., np.newaxis].astype(img.dtype))
52
- return Image.fromarray(img)
53
-
54
- # Function to apply blur effect
55
- def apply_blur(image, blur_factor):
56
- return image.filter(ImageFilter.GaussianBlur(radius=blur_factor))
57
-
58
- # Function to apply edge detection
59
- def apply_edge_detection(image):
60
- img = np.array(image)
61
- edges = cv2.Canny(img, 100, 200)
62
- return Image.fromarray(edges)
63
-
64
- # Function to add border
65
- def add_border(image, border_color, border_width):
66
- # Adding border using PIL ImageOps
67
- return ImageOps.expand(image, border=border_width, fill=border_color)
68
-
69
- # Function to add frame
70
- def add_frame(image, frame_style="Simple"):
71
  width, height = image.size
72
- if frame_style == "Simple":
73
- # Add simple black border
74
- frame_width = 30
75
- return ImageOps.expand(image, border=frame_width, fill="black")
76
- elif frame_style == "Ornate":
77
- # Add ornate frame (you can customize with more ornate styles)
78
- frame_width = 50
79
- return ImageOps.expand(image, border=frame_width, fill="gold")
80
- elif frame_style == "Rounded":
81
- # Add rounded frame (with radius)
82
- frame_width = 30
83
- image_with_frame = ImageOps.expand(image, border=frame_width, fill="blue")
84
- return image_with_frame.convert("RGBA").rotate(0, resample=Image.BICUBIC)
85
- elif frame_style == "Polaroid":
86
- # Add Polaroid-style frame (white bottom for caption)
87
- frame_width = 40
88
- image_with_frame = ImageOps.expand(image, border=frame_width, fill="white")
89
- bottom_margin = Image.new('RGB', (image_with_frame.width, 50), color='white')
90
- return Image.new('RGB', (image_with_frame.width, image_with_frame.height + bottom_margin.height), color='white').paste(image_with_frame, (0, 0)).paste(bottom_margin, (0, image_with_frame.height))
91
- elif frame_style == "Shadow":
92
- # Add shadow frame
93
- frame_width = 50
94
- shadow_image = Image.new("RGBA", (image.width + 2 * frame_width, image.height + 2 * frame_width), (0, 0, 0, 180))
95
- shadow_image.paste(image, (frame_width, frame_width))
96
- return shadow_image.convert("RGB")
97
- elif frame_style == "Vintage":
98
- # Add vintage-style frame with a distressed look (this can be more complex)
99
- frame_width = 30
100
- return ImageOps.expand(image, border=frame_width, fill="beige")
101
- elif frame_style == "Filmstrip":
102
- # Add a filmstrip frame with a perforated edge
103
- frame_width = 60
104
- return ImageOps.expand(image, border=frame_width, fill="black")
105
- elif frame_style == "Double Border":
106
- # Add a double border (one thin, one thick)
107
- outer_width = 60
108
- inner_width = 20
109
- image_with_outer_border = ImageOps.expand(image, border=outer_width, fill="black")
110
- return ImageOps.expand(image_with_outer_border, border=inner_width, fill="white")
111
- elif frame_style == "Metallic":
112
- # Add metallic frame (gold, silver, etc.)
113
- frame_width = 40
114
- return ImageOps.expand(image, border=frame_width, fill="gold")
115
- elif frame_style == "Heart-Shaped":
116
- # Add heart-shaped frame
117
- image_with_frame = image.resize((450, 450)) # Adjust for better fit
118
- return image_with_frame.convert("RGBA")
119
- elif frame_style == "Circular":
120
- # Add circular frame
121
- image_with_frame = image.resize((450, 450)) # Adjust for better fit
122
- return image_with_frame.convert("RGBA")
123
- elif frame_style == "Wooden":
124
- # Add wooden frame
125
- frame_width = 50
126
- return ImageOps.expand(image, border=frame_width, fill="saddlebrown")
127
- elif frame_style == "Neon Glow":
128
- # Add neon glow frame
129
- frame_width = 50
130
- neon_image = Image.new("RGBA", (image.width + 2 * frame_width, image.height + 2 * frame_width), (0, 255, 255, 255))
131
- neon_image.paste(image, (frame_width, frame_width))
132
- return neon_image.convert("RGB")
133
- elif frame_style == "Gradient":
134
- # Add gradient frame
135
- frame_width = 40
136
- return ImageOps.expand(image, border=frame_width, fill="gradient")
137
- elif frame_style == "Geometric":
138
- # Add geometric frame (triangles, hexagons, etc.)
139
- frame_width = 30
140
- return ImageOps.expand(image, border=frame_width, fill="purple")
141
-
142
- # Function to convert PIL image to bytes for download
143
- def pil_to_bytes(image, format="JPEG"):
144
- img_byte_arr = io.BytesIO()
145
- image.save(img_byte_arr, format=format)
146
- img_byte_arr.seek(0)
147
- return img_byte_arr
148
-
149
- # Streamlit app
150
  def main():
151
- st.title("Advanced Image Editor")
 
152
 
153
- # Upload image
154
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
155
 
156
  if uploaded_image is not None:
157
- # Open the uploaded image using PIL
158
  image = Image.open(uploaded_image)
159
 
160
- # Resize the image for better side-by-side display
161
- original_image_resized = image.resize((400, int(image.height * 400 / image.width)))
162
 
163
- # Apply filters
164
- st.sidebar.title("Adjust Filters")
165
-
166
- filter_option = st.sidebar.selectbox("Choose a filter", ("None", "Grayscale", "Brightness", "Contrast", "Saturation", "Sharpness", "Sepia", "Vignette", "Blur", "Edge Detection"))
167
-
168
- if filter_option == "Grayscale":
169
  image = apply_grayscale(image)
170
- elif filter_option == "Brightness":
171
- brightness_factor = st.sidebar.slider("Brightness Factor", 0.1, 2.0, 1.0)
172
- image = adjust_brightness(image, brightness_factor)
173
- elif filter_option == "Contrast":
174
- contrast_factor = st.sidebar.slider("Contrast Factor", 0.1, 2.0, 1.0)
175
- image = adjust_contrast(image, contrast_factor)
176
- elif filter_option == "Saturation":
177
- saturation_factor = st.sidebar.slider("Saturation Factor", 0.1, 2.0, 1.0)
178
- image = adjust_saturation(image, saturation_factor)
179
- elif filter_option == "Sharpness":
180
- sharpness_factor = st.sidebar.slider("Sharpness Factor", 0.1, 2.0, 1.0)
181
- image = apply_sharpness(image, sharpness_factor)
182
- elif filter_option == "Sepia":
183
- image = apply_sepia(image)
184
- elif filter_option == "Vignette":
185
- image = apply_vignette(image)
186
- elif filter_option == "Blur":
187
- blur_factor = st.sidebar.slider("Blur Radius", 0.1, 10.0, 2.0)
188
- image = apply_blur(image, blur_factor)
189
- elif filter_option == "Edge Detection":
190
- image = apply_edge_detection(image)
191
-
192
- # Add border or frame
193
- border_option = st.sidebar.selectbox("Add Border or Frame", ("None", "Border", "Frame"))
194
-
195
- if border_option == "Border":
196
- border_color = st.sidebar.color_picker("Pick a border color", "#000000")
197
- border_width = st.sidebar.slider("Border Width", 1, 100, 10)
198
- image = add_border(image, border_color, border_width)
199
- elif border_option == "Frame":
200
- frame_style = st.sidebar.selectbox("Choose a frame style", (
201
- "Simple", "Ornate", "Rounded", "Polaroid", "Shadow", "Vintage", "Filmstrip", "Double Border", "Metallic",
202
- "Heart-Shaped", "Circular", "Wooden", "Neon Glow", "Gradient", "Geometric"
203
- ))
204
- image = add_frame(image, frame_style)
205
-
206
- # Resize the filtered image for display
207
- modified_image_resized = image.resize((400, int(image.height * 400 / image.width)))
208
-
209
- # Create two columns to display images side by side
210
- col1, col2 = st.columns(2)
211
-
212
- # Display the original image in the first column
213
- with col1:
214
- st.image(original_image_resized, caption="Original Image", use_container_width=True)
215
-
216
- # Display the modified image in the second column
217
- with col2:
218
- st.image(modified_image_resized, caption="Modified Image", use_container_width=True)
219
-
220
- # Convert the edited image to bytes for download
221
- img_bytes = pil_to_bytes(image)
222
-
223
- # Button to download the edited image below the modified image
224
- with col2:
225
- st.download_button("Download Edited Image", img_bytes, "edited_image.jpg", "image/jpeg")
226
 
227
  if __name__ == "__main__":
228
  main()
 
1
  import streamlit as st
2
  from PIL import Image, ImageEnhance, ImageFilter, ImageOps
3
  import numpy as np
4
+ from io import BytesIO
 
5
 
6
  # Function to apply grayscale filter
7
  def apply_grayscale(image):
8
  return image.convert("L")
9
 
10
+ # Function to apply brightness adjustment
11
+ def apply_brightness(image, factor):
12
  enhancer = ImageEnhance.Brightness(image)
13
  return enhancer.enhance(factor)
14
 
15
+ # Function to apply contrast adjustment
16
+ def apply_contrast(image, factor):
17
  enhancer = ImageEnhance.Contrast(image)
18
  return enhancer.enhance(factor)
19
 
20
+ # Function to apply sharpness adjustment
 
 
 
 
 
21
  def apply_sharpness(image, factor):
22
  enhancer = ImageEnhance.Sharpness(image)
23
  return enhancer.enhance(factor)
24
 
25
+ # Function to apply blur filter
26
+ def apply_blur(image, blur_radius):
27
+ return image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
28
+
29
+ # Function to add border to image
30
+ def add_border(image, border_type, color, thickness):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  width, height = image.size
32
+ border_color = tuple(int(c) for c in color.split(','))
33
+ if border_type == "Solid Color":
34
+ return ImageOps.expand(image, border=thickness, fill=border_color)
35
+ elif border_type == "Dotted":
36
+ # Use dotted pattern
37
+ return ImageOps.expand(image, border=thickness, fill=border_color)
38
+ elif border_type == "Dashed":
39
+ return ImageOps.expand(image, border=thickness, fill=border_color)
40
+ elif border_type == "Double":
41
+ return ImageOps.expand(image, border=thickness, fill=border_color)
42
+ elif border_type == "Inset":
43
+ return ImageOps.expand(image, border=thickness, fill=border_color)
44
+ elif border_type == "Outset":
45
+ return ImageOps.expand(image, border=thickness, fill=border_color)
46
+ elif border_type == "Rounded Corner":
47
+ return ImageOps.expand(image, border=thickness, fill=border_color)
48
+ elif border_type == "Shadowed":
49
+ return ImageOps.expand(image, border=thickness, fill=border_color)
50
+ elif border_type == "Polaroid":
51
+ return ImageOps.expand(image, border=thickness, fill=border_color)
52
+ elif border_type == "Metallic":
53
+ return ImageOps.expand(image, border=thickness, fill=border_color)
54
+ elif border_type == "Wooden":
55
+ return ImageOps.expand(image, border=thickness, fill=border_color)
56
+ elif border_type == "Textured":
57
+ return ImageOps.expand(image, border=thickness, fill=border_color)
58
+ elif border_type == "Neon":
59
+ return ImageOps.expand(image, border=thickness, fill=border_color)
60
+ elif border_type == "Polka Dots":
61
+ return ImageOps.expand(image, border=thickness, fill=border_color)
62
+ elif border_type == "Stripe":
63
+ return ImageOps.expand(image, border=thickness, fill=border_color)
64
+ elif border_type == "Sketch":
65
+ return ImageOps.expand(image, border=thickness, fill=border_color)
66
+ elif border_type == "Lace":
67
+ return ImageOps.expand(image, border=thickness, fill=border_color)
68
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def main():
70
+ st.title("Image Editor")
71
+ st.sidebar.title("Enhance Image")
72
 
73
+ # Upload Image
74
+ uploaded_image = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
75
 
76
  if uploaded_image is not None:
 
77
  image = Image.open(uploaded_image)
78
 
79
+ # Show original image
80
+ st.image(image, caption="Original Image", use_container_width=True)
81
 
82
+ # Grayscale option
83
+ grayscale = st.sidebar.checkbox("Apply Grayscale")
84
+ if grayscale:
 
 
 
85
  image = apply_grayscale(image)
86
+
87
+ # Brightness adjustment
88
+ brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
89
+ image = apply_brightness(image, brightness_factor)
90
+
91
+ # Contrast adjustment
92
+ contrast_factor = st.sidebar.slider("Adjust Contrast", 0.0, 2.0, 1.0)
93
+ image = apply_contrast(image, contrast_factor)
94
+
95
+ # Sharpness adjustment
96
+ sharpness_factor = st.sidebar.slider("Adjust Sharpness", 0.0, 2.0, 1.0)
97
+ image = apply_sharpness(image, sharpness_factor)
98
+
99
+ # Blur effect
100
+ blur_radius = st.sidebar.slider("Blur Radius", 0, 10, 0)
101
+ if blur_radius > 0:
102
+ image = apply_blur(image, blur_radius)
103
+
104
+ # Border options
105
+ border_type = st.sidebar.selectbox("Select Border Type", [
106
+ "Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset", "Rounded Corner",
107
+ "Shadowed", "Polaroid", "Metallic", "Wooden", "Textured", "Neon", "Polka Dots",
108
+ "Stripe", "Sketch", "Lace"])
109
+
110
+ border_color = st.sidebar.color_picker("Pick a Border Color", "#000000")
111
+ border_thickness = st.sidebar.slider("Select Border Thickness", 0, 50, 10)
112
+
113
+ image = add_border(image, border_type, border_color[1:], border_thickness)
114
+
115
+ # Display the modified image
116
+ st.image(image, caption="Modified Image", use_container_width=True)
117
+
118
+ # Download button
119
+ buffered = BytesIO()
120
+ image.save(buffered, format="JPEG")
121
+ buffered.seek(0)
122
+
123
+ st.sidebar.download_button(
124
+ label="Download Edited Image",
125
+ data=buffered,
126
+ file_name="edited_image.jpg",
127
+ mime="image/jpeg"
128
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  if __name__ == "__main__":
131
  main()