HFUsman commited on
Commit
3295adf
·
verified ·
1 Parent(s): 44920e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -107
app.py CHANGED
@@ -1,86 +1,48 @@
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 convert hex color to RGB
7
  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 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,66 +75,55 @@ def add_border(image, border_type, color, thickness):
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()
 
1
  import streamlit as st
2
+ from PIL import Image, ImageEnhance, ImageOps, ImageFilter
3
  import numpy as np
4
  from io import BytesIO
5
 
6
  # Function to convert hex color to RGB
7
  def hex_to_rgb(hex_color):
8
  hex_color = hex_color.lstrip('#') # Remove the '#' if present
9
+ # Check if hex color length is 6 (for #RRGGBB format)
10
+ if len(hex_color) == 6:
11
+ return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Convert to RGB
12
+ else:
13
+ raise ValueError("Invalid hex color format")
14
 
15
+ # Function to apply grayscale
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def apply_grayscale(image):
17
+ return image.convert('L')
18
 
19
+ # Function to adjust brightness
20
+ def adjust_brightness(image, factor):
21
  enhancer = ImageEnhance.Brightness(image)
22
  return enhancer.enhance(factor)
23
 
24
+ # Function to adjust contrast
25
+ def adjust_contrast(image, factor):
26
  enhancer = ImageEnhance.Contrast(image)
27
  return enhancer.enhance(factor)
28
 
29
+ # Function to apply blur
 
 
 
 
 
30
  def apply_blur(image, blur_radius):
31
+ return image.filter(ImageFilter.GaussianBlur(blur_radius))
32
 
33
  # Function to add border to image
34
  def add_border(image, border_type, color, thickness):
35
  width, height = image.size
36
+ try:
37
+ border_color = hex_to_rgb(color) # Convert hex color to RGB tuple
38
+ except ValueError as e:
39
+ st.error(f"Error with the border color: {e}")
40
+ return image
41
+
42
+ # Applying the border based on the selected type
43
  if border_type == "Solid Color":
44
  return ImageOps.expand(image, border=thickness, fill=border_color)
45
  elif border_type == "Dotted":
 
46
  return ImageOps.expand(image, border=thickness, fill=border_color)
47
  elif border_type == "Dashed":
48
  return ImageOps.expand(image, border=thickness, fill=border_color)
 
75
  elif border_type == "Lace":
76
  return ImageOps.expand(image, border=thickness, fill=border_color)
77
 
78
+ # Main function to handle Streamlit app
79
  def main():
80
  st.title("Image Editor")
 
 
 
 
81
 
82
+ # Upload image
83
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
84
+
85
  if uploaded_image is not None:
86
  image = Image.open(uploaded_image)
87
+ st.image(image, caption='Uploaded Image', use_container_width=True)
88
+
89
+ # Sidebar options
90
+ st.sidebar.title("Image Editing Options")
91
+
92
+ # Filter Options
93
+ filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast", "Blur"])
94
+
95
+ if filter_option == "Grayscale":
96
  image = apply_grayscale(image)
97
+ elif filter_option == "Brightness":
98
+ brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
99
+ image = adjust_brightness(image, brightness_factor)
100
+ elif filter_option == "Contrast":
101
+ contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
102
+ image = adjust_contrast(image, contrast_factor)
103
+ elif filter_option == "Blur":
104
+ blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
 
 
 
 
 
 
 
 
105
  image = apply_blur(image, blur_radius)
106
 
107
  # Border options
108
+ border_type = st.sidebar.selectbox("Choose Border Type", [
109
+ "None", "Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset",
110
+ "Rounded Corner", "Shadowed", "Polaroid", "Metallic", "Wooden", "Textured",
111
+ "Neon", "Polka Dots", "Stripe", "Sketch", "Lace"
112
+ ])
113
+ if border_type != "None":
114
+ border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
115
+ border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
116
+ image = add_border(image, border_type, border_color, border_thickness)
117
+
118
+ # Display the edited image
119
+ st.image(image, caption='Edited Image', use_container_width=True)
120
+
121
+ # Download button for edited image
122
  buffered = BytesIO()
123
+ image.save(buffered, format="PNG")
124
  buffered.seek(0)
125
+ st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
126
 
 
 
 
 
 
 
127
 
128
  if __name__ == "__main__":
129
  main()