HFUsman commited on
Commit
44920e6
·
verified ·
1 Parent(s): 729b3f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -48
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance, ImageOps, ImageFilter
3
  import numpy as np
4
  from io import BytesIO
5
 
@@ -8,32 +8,79 @@ def hex_to_rgb(hex_color):
8
  hex_color = hex_color.lstrip('#') # Remove the '#' if present
9
  return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Convert to RGB
10
 
11
- # Function to apply grayscale
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def apply_grayscale(image):
13
- return image.convert('L')
14
 
15
- # Function to adjust brightness
16
- def adjust_brightness(image, factor):
17
  enhancer = ImageEnhance.Brightness(image)
18
  return enhancer.enhance(factor)
19
 
20
- # Function to adjust contrast
21
- def adjust_contrast(image, factor):
22
  enhancer = ImageEnhance.Contrast(image)
23
  return enhancer.enhance(factor)
24
 
25
- # Function to apply blur
 
 
 
 
 
26
  def apply_blur(image, blur_radius):
27
- return image.filter(ImageFilter.GaussianBlur(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 = hex_to_rgb(color) # Convert hex color to RGB tuple
33
-
34
  if border_type == "Solid Color":
35
  return ImageOps.expand(image, border=thickness, fill=border_color)
36
  elif border_type == "Dotted":
 
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)
@@ -66,55 +113,66 @@ def add_border(image, border_type, color, thickness):
66
  elif border_type == "Lace":
67
  return ImageOps.expand(image, border=thickness, fill=border_color)
68
 
69
- # Main function to handle Streamlit app
70
  def main():
71
  st.title("Image Editor")
 
 
 
 
72
 
73
- # Upload image
74
- uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
75
-
76
  if uploaded_image is not None:
77
  image = Image.open(uploaded_image)
78
- st.image(image, caption='Uploaded Image', use_container_width=True)
79
-
80
- # Sidebar options
81
- st.sidebar.title("Image Editing Options")
82
-
83
- # Filter Options
84
- filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast", "Blur"])
85
-
86
- if filter_option == "Grayscale":
87
  image = apply_grayscale(image)
88
- elif filter_option == "Brightness":
89
- brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
90
- image = adjust_brightness(image, brightness_factor)
91
- elif filter_option == "Contrast":
92
- contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
93
- image = adjust_contrast(image, contrast_factor)
94
- elif filter_option == "Blur":
95
- blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
 
 
 
 
 
 
 
 
96
  image = apply_blur(image, blur_radius)
97
 
98
  # Border options
99
- border_type = st.sidebar.selectbox("Choose Border Type", [
100
- "None", "Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset",
101
- "Rounded Corner", "Shadowed", "Polaroid", "Metallic", "Wooden", "Textured",
102
- "Neon", "Polka Dots", "Stripe", "Sketch", "Lace"
103
- ])
104
- if border_type != "None":
105
- border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
106
- border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
107
- image = add_border(image, border_type, border_color, border_thickness)
108
-
109
- # Display the edited image
110
- st.image(image, caption='Edited Image', use_container_width=True)
111
-
112
- # Download button for edited image
113
  buffered = BytesIO()
114
- image.save(buffered, format="PNG")
115
  buffered.seek(0)
116
- st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
117
 
 
 
 
 
 
 
118
 
119
  if __name__ == "__main__":
120
  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
 
 
8
  hex_color = hex_color.lstrip('#') # Remove the '#' if present
9
  return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Convert to RGB
10
 
11
+ # Function to add border to image
12
+ def add_border(image, border_type, color, thickness):
13
+ width, height = image.size
14
+ border_color = hex_to_rgb(color) # Convert hex color to RGB tuple
15
+
16
+ if border_type == "Solid Color":
17
+ return ImageOps.expand(image, border=thickness, fill=border_color)
18
+ elif border_type == "Dotted":
19
+ # Use dotted pattern (this is a simple approximation)
20
+ return ImageOps.expand(image, border=thickness, fill=border_color)
21
+ elif border_type == "Dashed":
22
+ return ImageOps.expand(image, border=thickness, fill=border_color)
23
+ elif border_type == "Double":
24
+ return ImageOps.expand(image, border=thickness, fill=border_color)
25
+ elif border_type == "Inset":
26
+ return ImageOps.expand(image, border=thickness, fill=border_color)
27
+ elif border_type == "Outset":
28
+ return ImageOps.expand(image, border=thickness, fill=border_color)
29
+ elif border_type == "Rounded Corner":
30
+ return ImageOps.expand(image, border=thickness, fill=border_color)
31
+ elif border_type == "Shadowed":
32
+ return ImageOps.expand(image, border=thickness, fill=border_color)
33
+ elif border_type == "Polaroid":
34
+ return ImageOps.expand(image, border=thickness, fill=border_color)
35
+ elif border_type == "Metallic":
36
+ return ImageOps.expand(image, border=thickness, fill=border_color)
37
+ elif border_type == "Wooden":
38
+ return ImageOps.expand(image, border=thickness, fill=border_color)
39
+ elif border_type == "Textured":
40
+ return ImageOps.expand(image, border=thickness, fill=border_color)
41
+ elif border_type == "Neon":
42
+ return ImageOps.expand(image, border=thickness, fill=border_color)
43
+ elif border_type == "Polka Dots":
44
+ return ImageOps.expand(image, border=thickness, fill=border_color)
45
+ elif border_type == "Stripe":
46
+ return ImageOps.expand(image, border=thickness, fill=border_color)
47
+ elif border_type == "Sketch":
48
+ return ImageOps.expand(image, border=thickness, fill=border_color)
49
+ elif border_type == "Lace":
50
+ return ImageOps.expand(image, border=thickness, fill=border_color)
51
+
52
+
53
+ # Function to apply grayscale filter
54
  def apply_grayscale(image):
55
+ return image.convert("L")
56
 
57
+ # Function to apply brightness adjustment
58
+ def apply_brightness(image, factor):
59
  enhancer = ImageEnhance.Brightness(image)
60
  return enhancer.enhance(factor)
61
 
62
+ # Function to apply contrast adjustment
63
+ def apply_contrast(image, factor):
64
  enhancer = ImageEnhance.Contrast(image)
65
  return enhancer.enhance(factor)
66
 
67
+ # Function to apply sharpness adjustment
68
+ def apply_sharpness(image, factor):
69
+ enhancer = ImageEnhance.Sharpness(image)
70
+ return enhancer.enhance(factor)
71
+
72
+ # Function to apply blur filter
73
  def apply_blur(image, blur_radius):
74
+ return image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
75
 
76
  # Function to add border to image
77
  def add_border(image, border_type, color, thickness):
78
  width, height = image.size
79
+ border_color = tuple(int(c) for c in color.split(','))
 
80
  if border_type == "Solid Color":
81
  return ImageOps.expand(image, border=thickness, fill=border_color)
82
  elif border_type == "Dotted":
83
+ # Use dotted pattern
84
  return ImageOps.expand(image, border=thickness, fill=border_color)
85
  elif border_type == "Dashed":
86
  return ImageOps.expand(image, border=thickness, fill=border_color)
 
113
  elif border_type == "Lace":
114
  return ImageOps.expand(image, border=thickness, fill=border_color)
115
 
 
116
  def main():
117
  st.title("Image Editor")
118
+ st.sidebar.title("Enhance Image")
119
+
120
+ # Upload Image
121
+ uploaded_image = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
122
 
 
 
 
123
  if uploaded_image is not None:
124
  image = Image.open(uploaded_image)
125
+
126
+ # Show original image
127
+ st.image(image, caption="Original Image", use_container_width=True)
128
+
129
+ # Grayscale option
130
+ grayscale = st.sidebar.checkbox("Apply Grayscale")
131
+ if grayscale:
 
 
132
  image = apply_grayscale(image)
133
+
134
+ # Brightness adjustment
135
+ brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
136
+ image = apply_brightness(image, brightness_factor)
137
+
138
+ # Contrast adjustment
139
+ contrast_factor = st.sidebar.slider("Adjust Contrast", 0.0, 2.0, 1.0)
140
+ image = apply_contrast(image, contrast_factor)
141
+
142
+ # Sharpness adjustment
143
+ sharpness_factor = st.sidebar.slider("Adjust Sharpness", 0.0, 2.0, 1.0)
144
+ image = apply_sharpness(image, sharpness_factor)
145
+
146
+ # Blur effect
147
+ blur_radius = st.sidebar.slider("Blur Radius", 0, 10, 0)
148
+ if blur_radius > 0:
149
  image = apply_blur(image, blur_radius)
150
 
151
  # Border options
152
+ border_type = st.sidebar.selectbox("Select Border Type", [
153
+ "Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset", "Rounded Corner",
154
+ "Shadowed", "Polaroid", "Metallic", "Wooden", "Textured", "Neon", "Polka Dots",
155
+ "Stripe", "Sketch", "Lace"])
156
+
157
+ border_color = st.sidebar.color_picker("Pick a Border Color", "#000000")
158
+ border_thickness = st.sidebar.slider("Select Border Thickness", 0, 50, 10)
159
+
160
+ image = add_border(image, border_type, border_color[1:], border_thickness)
161
+
162
+ # Display the modified image
163
+ st.image(image, caption="Modified Image", use_container_width=True)
164
+
165
+ # Download button
166
  buffered = BytesIO()
167
+ image.save(buffered, format="JPEG")
168
  buffered.seek(0)
 
169
 
170
+ st.sidebar.download_button(
171
+ label="Download Edited Image",
172
+ data=buffered,
173
+ file_name="edited_image.jpg",
174
+ mime="image/jpeg"
175
+ )
176
 
177
  if __name__ == "__main__":
178
  main()