Talha1786 commited on
Commit
c28dd2f
·
verified ·
1 Parent(s): 1001acf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -11
app.py CHANGED
@@ -1,18 +1,41 @@
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance
3
  import numpy as np
4
 
5
- # Function to apply grayscale filter
6
  def apply_grayscale(image):
7
  return image.convert("L")
8
 
9
- # Function to adjust brightness
10
  def adjust_brightness(image, factor):
11
  enhancer = ImageEnhance.Brightness(image)
12
  return enhancer.enhance(factor)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # Streamlit UI
15
- st.title("Simple Image Editor")
16
 
17
  # Upload image
18
  uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
@@ -24,16 +47,50 @@ if uploaded_file is not None:
24
  # Display original image
25
  st.image(image, caption="Original Image", use_column_width=True)
26
 
27
- # Select filter to apply
28
- filter_option = st.selectbox("Choose a filter", ("None", "Grayscale", "Brightness"))
29
 
30
- # Apply selected filter
31
- if filter_option == "Grayscale":
32
- image = apply_grayscale(image)
33
- elif filter_option == "Brightness":
34
- factor = st.slider("Adjust brightness", 0.5, 2.0, 1.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  image = adjust_brightness(image, factor)
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # Display edited image
38
  st.image(image, caption="Edited Image", use_column_width=True)
39
 
@@ -46,3 +103,4 @@ if uploaded_file is not None:
46
  file_name="edited_image.png",
47
  mime="image/png"
48
  )
 
 
1
  import streamlit as st
2
+ from PIL import Image, ImageEnhance, ImageFilter
3
  import numpy as np
4
 
5
+ # Helper functions for filters and enhancements
6
  def apply_grayscale(image):
7
  return image.convert("L")
8
 
 
9
  def adjust_brightness(image, factor):
10
  enhancer = ImageEnhance.Brightness(image)
11
  return enhancer.enhance(factor)
12
 
13
+ def adjust_contrast(image, factor):
14
+ enhancer = ImageEnhance.Contrast(image)
15
+ return enhancer.enhance(factor)
16
+
17
+ def adjust_saturation(image, factor):
18
+ enhancer = ImageEnhance.Color(image)
19
+ return enhancer.enhance(factor)
20
+
21
+ def apply_blur(image, radius):
22
+ return image.filter(ImageFilter.GaussianBlur(radius))
23
+
24
+ def apply_edge_detection(image):
25
+ return image.filter(ImageFilter.FIND_EDGES)
26
+
27
+ def rotate_image(image, angle):
28
+ return image.rotate(angle, expand=True)
29
+
30
+ def flip_image(image, direction):
31
+ if direction == "Horizontal":
32
+ return image.transpose(Image.FLIP_LEFT_RIGHT)
33
+ elif direction == "Vertical":
34
+ return image.transpose(Image.FLIP_TOP_BOTTOM)
35
+ return image
36
+
37
  # Streamlit UI
38
+ st.title("Advanced Image Editor")
39
 
40
  # Upload image
41
  uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
 
47
  # Display original image
48
  st.image(image, caption="Original Image", use_column_width=True)
49
 
50
+ # Sidebar for feature selection
51
+ st.sidebar.title("Image Editing Options")
52
 
53
+ # Basic transformations
54
+ if st.sidebar.checkbox("Crop"):
55
+ st.write("Crop feature will be implemented here.")
56
+
57
+ if st.sidebar.checkbox("Resize"):
58
+ width = st.sidebar.number_input("Width", min_value=10, value=image.width)
59
+ height = st.sidebar.number_input("Height", min_value=10, value=image.height)
60
+ image = image.resize((width, height))
61
+
62
+ if st.sidebar.checkbox("Rotate"):
63
+ angle = st.sidebar.slider("Angle", 0, 360, 0)
64
+ image = rotate_image(image, angle)
65
+
66
+ if st.sidebar.checkbox("Flip"):
67
+ flip_direction = st.sidebar.radio("Direction", ("Horizontal", "Vertical"))
68
+ image = flip_image(image, flip_direction)
69
+
70
+ # Enhancements
71
+ if st.sidebar.checkbox("Adjust Brightness"):
72
+ factor = st.sidebar.slider("Brightness", 0.5, 2.0, 1.0)
73
  image = adjust_brightness(image, factor)
74
 
75
+ if st.sidebar.checkbox("Adjust Contrast"):
76
+ factor = st.sidebar.slider("Contrast", 0.5, 2.0, 1.0)
77
+ image = adjust_contrast(image, factor)
78
+
79
+ if st.sidebar.checkbox("Adjust Saturation"):
80
+ factor = st.sidebar.slider("Saturation", 0.5, 2.0, 1.0)
81
+ image = adjust_saturation(image, factor)
82
+
83
+ # Filters
84
+ if st.sidebar.checkbox("Grayscale"):
85
+ image = apply_grayscale(image)
86
+
87
+ if st.sidebar.checkbox("Blur"):
88
+ radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
89
+ image = apply_blur(image, radius)
90
+
91
+ if st.sidebar.checkbox("Edge Detection"):
92
+ image = apply_edge_detection(image)
93
+
94
  # Display edited image
95
  st.image(image, caption="Edited Image", use_column_width=True)
96
 
 
103
  file_name="edited_image.png",
104
  mime="image/png"
105
  )
106
+