HFUsman commited on
Commit
ccd1117
·
verified ·
1 Parent(s): d5fa4bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -124
app.py CHANGED
@@ -3,131 +3,156 @@ 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)
49
- elif border_type == "Double":
50
- return ImageOps.expand(image, border=thickness, fill=border_color)
51
- elif border_type == "Inset":
52
- return ImageOps.expand(image, border=thickness, fill=border_color)
53
- elif border_type == "Outset":
54
- return ImageOps.expand(image, border=thickness, fill=border_color)
55
- elif border_type == "Rounded Corner":
56
- return ImageOps.expand(image, border=thickness, fill=border_color)
57
- elif border_type == "Shadowed":
58
- return ImageOps.expand(image, border=thickness, fill=border_color)
59
- elif border_type == "Polaroid":
60
- return ImageOps.expand(image, border=thickness, fill=border_color)
61
- elif border_type == "Metallic":
62
- return ImageOps.expand(image, border=thickness, fill=border_color)
63
- elif border_type == "Wooden":
64
- return ImageOps.expand(image, border=thickness, fill=border_color)
65
- elif border_type == "Textured":
66
- return ImageOps.expand(image, border=thickness, fill=border_color)
67
- elif border_type == "Neon":
68
- return ImageOps.expand(image, border=thickness, fill=border_color)
69
- elif border_type == "Polka Dots":
70
- return ImageOps.expand(image, border=thickness, fill=border_color)
71
- elif border_type == "Stripe":
72
- return ImageOps.expand(image, border=thickness, fill=border_color)
73
- elif border_type == "Sketch":
74
- 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
-
88
- # Sidebar options
89
- st.sidebar.title("Image Editing Options")
90
-
91
- # Filter Options
92
- filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast", "Blur"])
93
-
94
- if filter_option == "Grayscale":
95
- image = apply_grayscale(image)
96
- elif filter_option == "Brightness":
97
- brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
98
- image = adjust_brightness(image, brightness_factor)
99
- elif filter_option == "Contrast":
100
- contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
101
- image = adjust_contrast(image, contrast_factor)
102
- elif filter_option == "Blur":
103
- blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
104
- image = apply_blur(image, blur_radius)
105
-
106
- # Border options
107
- border_type = st.sidebar.selectbox("Choose Border Type", [
108
- "None", "Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset",
109
- "Rounded Corner", "Shadowed", "Polaroid", "Metallic", "Wooden", "Textured",
110
- "Neon", "Polka Dots", "Stripe", "Sketch", "Lace"
111
- ])
112
- if border_type != "None":
113
- border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
114
- border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
115
- image = add_border(image, border_type, border_color, border_thickness)
116
-
117
- # Display the original and edited images side by side
118
- col1, col2 = st.columns(2)
119
-
120
- with col1:
121
- st.image(uploaded_image, caption='Original Image', use_container_width=True)
122
-
123
- with col2:
124
- st.image(image, caption='Edited Image', use_container_width=True)
125
-
126
- # Download button for edited image
127
- buffered = BytesIO()
128
- image.save(buffered, format="PNG")
129
- buffered.seek(0)
130
- st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
131
 
132
 
133
  if __name__ == "__main__":
 
3
  import numpy as np
4
  from io import BytesIO
5
 
6
+
7
+ # Abstract Class for Image Editor
8
+ class ImageEditor:
9
+ def __init__(self, image):
10
+ self.image = image
11
+
12
+ def apply(self):
13
+ raise NotImplementedError("Subclasses should implement this method")
14
+
15
+
16
+ # Grayscale Filter Class
17
+ class GrayscaleFilter(ImageEditor):
18
+ def apply(self):
19
+ return self.image.convert("L")
20
+
21
+
22
+ # Brightness Adjustment Class
23
+ class BrightnessFilter(ImageEditor):
24
+ def __init__(self, image, factor):
25
+ super().__init__(image)
26
+ self.factor = factor
27
+
28
+ def apply(self):
29
+ enhancer = ImageEnhance.Brightness(self.image)
30
+ return enhancer.enhance(self.factor)
31
+
32
+
33
+ # Contrast Adjustment Class
34
+ class ContrastFilter(ImageEditor):
35
+ def __init__(self, image, factor):
36
+ super().__init__(image)
37
+ self.factor = factor
38
+
39
+ def apply(self):
40
+ enhancer = ImageEnhance.Contrast(self.image)
41
+ return enhancer.enhance(self.factor)
42
+
43
+
44
+ # Blur Effect Class
45
+ class BlurFilter(ImageEditor):
46
+ def __init__(self, image, radius):
47
+ super().__init__(image)
48
+ self.radius = radius
49
+
50
+ def apply(self):
51
+ return self.image.filter(ImageFilter.GaussianBlur(self.radius))
52
+
53
+
54
+ # Border Class
55
+ class Border:
56
+ def __init__(self, image, border_type, color, thickness):
57
+ self.image = image
58
+ self.border_type = border_type
59
+ self.color = color
60
+ self.thickness = thickness
61
+
62
+ def add_border(self):
63
+ try:
64
+ border_color = self.hex_to_rgb(self.color)
65
+ except ValueError:
66
+ st.error("Invalid color format. Please use Hex color.")
67
+ return self.image
68
+
69
+ if self.border_type == "Solid Color":
70
+ return ImageOps.expand(self.image, border=self.thickness, fill=border_color)
71
+ # Add other border types if needed
72
+
73
+ return self.image
74
+
75
+ @staticmethod
76
+ def hex_to_rgb(hex_color):
77
+ hex_color = hex_color.lstrip('#')
78
+ if len(hex_color) == 6:
79
+ return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
80
+ else:
81
+ raise ValueError("Invalid hex color format")
82
+
83
+
84
+ # Main Application Class
85
+ class ImageApp:
86
+ def __init__(self):
87
+ self.image = None
88
+ self.stored_image = None
89
+
90
+ def load_image(self):
91
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
92
+ if uploaded_image:
93
+ self.image = Image.open(uploaded_image)
94
+ self.stored_image = self.image.copy()
95
+
96
+ def apply_filters(self):
97
+ if self.image:
98
+ # Sidebar Filters
99
+ filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast", "Blur"])
100
+ if filter_option == "Grayscale":
101
+ self.image = GrayscaleFilter(self.image).apply()
102
+ elif filter_option == "Brightness":
103
+ brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
104
+ self.image = BrightnessFilter(self.image, brightness_factor).apply()
105
+ elif filter_option == "Contrast":
106
+ contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
107
+ self.image = ContrastFilter(self.image, contrast_factor).apply()
108
+ elif filter_option == "Blur":
109
+ blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
110
+ self.image = BlurFilter(self.image, blur_radius).apply()
111
+
112
+ def add_border(self):
113
+ if self.image:
114
+ border_type = st.sidebar.selectbox("Choose Border Type", ["None", "Solid Color"])
115
+ if border_type != "None":
116
+ border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
117
+ border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
118
+ border = Border(self.image, border_type, border_color, border_thickness)
119
+ self.image = border.add_border()
120
+
121
+ def display_images(self):
122
+ if self.image:
123
+ col1, col2 = st.columns(2)
124
+ with col1:
125
+ st.image(self.stored_image, caption="Original Image", use_container_width=True)
126
+ with col2:
127
+ st.image(self.image, caption="Edited Image", use_container_width=True)
128
+
129
+ def download_button(self):
130
+ if self.image:
131
+ buffered = BytesIO()
132
+ self.image.save(buffered, format="PNG")
133
+ buffered.seek(0)
134
+ st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
135
+
136
+
137
+ # Main function to run the app
138
  def main():
139
+ st.title("Modern Image Editor")
140
+ app = ImageApp()
141
+
142
+ # Load the image
143
+ app.load_image()
144
+
145
+ # Apply filters
146
+ app.apply_filters()
147
+
148
+ # Add borders if selected
149
+ app.add_border()
150
+
151
+ # Display images side by side
152
+ app.display_images()
153
+
154
+ # Provide download option
155
+ app.download_button()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
 
158
  if __name__ == "__main__":