aika42 commited on
Commit
757cd20
·
verified ·
1 Parent(s): b3c12ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -57
app.py CHANGED
@@ -17,7 +17,7 @@ with col1:
17
 
18
  image = None
19
  filtered_image = None
20
- image_copy = None
21
 
22
  if uploaded_file:
23
  # Size check
@@ -30,87 +30,94 @@ if uploaded_file:
30
  st.error("❌ File too large! Must be under 5MB.")
31
  else:
32
  image = Image.open(uploaded_file)
33
- image_copy = image.copy() # Keep a copy for modification
34
 
35
  with col2:
36
- st.header("🎛️ Select Filters")
37
- # Use checkboxes for multiple filters
38
- grayscale = st.checkbox("Grayscale")
39
- invert = st.checkbox("Invert Colors")
40
- sepia = st.checkbox("Sepia")
41
- blur = st.checkbox("Blur")
42
- edges = st.checkbox("Edge Detection")
43
- posterize = st.checkbox("Posterize")
44
- emboss = st.checkbox("Emboss")
45
- solarize = st.checkbox("Solarize")
46
- brightness = st.checkbox("Brightness Boost")
47
- contrast = st.checkbox("Contrast Boost")
48
- flip_h = st.checkbox("Flip Horizontal")
49
- flip_v = st.checkbox("Flip Vertical")
50
- rotate = st.checkbox("Rotate 90°")
51
 
52
  with col3:
53
  st.header("🔍 Preview Output")
54
 
55
- # Start with the original copy of the image
56
- filtered_image = image_copy.copy()
 
57
 
58
- # Apply the selected filters one by one
59
- if grayscale:
60
- filtered_image = ImageOps.grayscale(filtered_image)
 
 
61
 
62
- if invert:
63
- if filtered_image.mode != "RGB":
64
- filtered_image = filtered_image.convert("RGB")
65
- filtered_image = ImageOps.invert(filtered_image)
66
-
67
- if sepia:
68
- width, height = filtered_image.size
69
- pixels = filtered_image.load()
70
  for py in range(height):
71
  for px in range(width):
72
- r, g, b = filtered_image.getpixel((px, py))
73
 
74
  tr = int(0.393 * r + 0.769 * g + 0.189 * b)
75
  tg = int(0.349 * r + 0.686 * g + 0.168 * b)
76
  tb = int(0.272 * r + 0.534 * g + 0.131 * b)
77
 
78
- if tr > 255: tr = 255
79
- if tg > 255: tg = 255
80
- if tb > 255: tb = 255
 
 
 
 
81
 
82
  pixels[px, py] = (tr, tg, tb)
 
 
 
 
 
 
83
 
84
- if blur:
85
- filtered_image = filtered_image.filter(ImageFilter.GaussianBlur(radius=5))
 
86
 
87
- if edges:
88
- filtered_image = filtered_image.filter(ImageFilter.FIND_EDGES)
 
89
 
90
- if posterize:
91
- filtered_image = ImageOps.posterize(filtered_image, 4)
 
92
 
93
- if emboss:
94
- filtered_image = filtered_image.filter(ImageFilter.EMBOSS)
 
95
 
96
- if solarize:
97
- filtered_image = ImageOps.solarize(filtered_image, threshold=128)
 
 
98
 
99
- if brightness:
100
- enhancer = ImageEnhance.Brightness(filtered_image)
101
- filtered_image = enhancer.enhance(2)
 
102
 
103
- if contrast:
104
- enhancer = ImageEnhance.Contrast(filtered_image)
105
- filtered_image = enhancer.enhance(2)
106
 
107
- if flip_h:
108
- filtered_image = filtered_image.transpose(Image.FLIP_LEFT_RIGHT)
 
109
 
110
- if flip_v:
111
- filtered_image = filtered_image.transpose(Image.FLIP_TOP_BOTTOM)
 
112
 
113
- if rotate:
114
- filtered_image = filtered_image.rotate(90)
115
 
116
- st.image(filtered_image, caption="Filtered Image", use_container_width=True)
 
17
 
18
  image = None
19
  filtered_image = None
20
+ filter_option = "None"
21
 
22
  if uploaded_file:
23
  # Size check
 
30
  st.error("❌ File too large! Must be under 5MB.")
31
  else:
32
  image = Image.open(uploaded_file)
 
33
 
34
  with col2:
35
+ st.header("🎛️ Select Filter")
36
+ filter_option = st.selectbox("Choose a filter:", [
37
+ "None", "Grayscale", "Invert Colors", "Sepia", "Blur", "Edge Detection",
38
+ "Posterize", "Emboss", "Solarize", "Brightness Boost", "Contrast Boost",
39
+ "Flip Horizontal", "Flip Vertical", "Rotate 90°"
40
+ ])
 
 
 
 
 
 
 
 
 
41
 
42
  with col3:
43
  st.header("🔍 Preview Output")
44
 
45
+ if filter_option == "Grayscale":
46
+ filtered_image = ImageOps.grayscale(image)
47
+ st.image(filtered_image, caption="Grayscale", use_container_width=True)
48
 
49
+ elif filter_option == "Invert Colors":
50
+ if image.mode != "RGB":
51
+ image = image.convert("RGB")
52
+ filtered_image = ImageOps.invert(image)
53
+ st.image(filtered_image, caption="Inverted", use_container_width=True)
54
 
55
+ elif filter_option == "Sepia":
56
+ # Sepia filter
57
+ width, height = image.size
58
+ pixels = image.load() # create the pixel map
 
 
 
 
59
  for py in range(height):
60
  for px in range(width):
61
+ r, g, b = image.getpixel((px, py))
62
 
63
  tr = int(0.393 * r + 0.769 * g + 0.189 * b)
64
  tg = int(0.349 * r + 0.686 * g + 0.168 * b)
65
  tb = int(0.272 * r + 0.534 * g + 0.131 * b)
66
 
67
+ # Apply filter
68
+ if tr > 255:
69
+ tr = 255
70
+ if tg > 255:
71
+ tg = 255
72
+ if tb > 255:
73
+ tb = 255
74
 
75
  pixels[px, py] = (tr, tg, tb)
76
+ filtered_image = image
77
+ st.image(filtered_image, caption="Sepia", use_container_width=True)
78
+
79
+ elif filter_option == "Blur":
80
+ filtered_image = image.filter(ImageFilter.GaussianBlur(radius=5))
81
+ st.image(filtered_image, caption="Blurred", use_container_width=True)
82
 
83
+ elif filter_option == "Edge Detection":
84
+ filtered_image = image.filter(ImageFilter.FIND_EDGES)
85
+ st.image(filtered_image, caption="Edge Detection", use_container_width=True)
86
 
87
+ elif filter_option == "Posterize":
88
+ filtered_image = ImageOps.posterize(image, 4)
89
+ st.image(filtered_image, caption="Posterized", use_container_width=True)
90
 
91
+ elif filter_option == "Emboss":
92
+ filtered_image = image.filter(ImageFilter.EMBOSS)
93
+ st.image(filtered_image, caption="Emboss", use_container_width=True)
94
 
95
+ elif filter_option == "Solarize":
96
+ filtered_image = ImageOps.solarize(image, threshold=128)
97
+ st.image(filtered_image, caption="Solarized", use_container_width=True)
98
 
99
+ elif filter_option == "Brightness Boost":
100
+ enhancer = ImageEnhance.Brightness(image)
101
+ filtered_image = enhancer.enhance(2) # increase brightness
102
+ st.image(filtered_image, caption="Brightness Boosted", use_container_width=True)
103
 
104
+ elif filter_option == "Contrast Boost":
105
+ enhancer = ImageEnhance.Contrast(image)
106
+ filtered_image = enhancer.enhance(2) # increase contrast
107
+ st.image(filtered_image, caption="Contrast Boosted", use_container_width=True)
108
 
109
+ elif filter_option == "Flip Horizontal":
110
+ filtered_image = image.transpose(Image.FLIP_LEFT_RIGHT)
111
+ st.image(filtered_image, caption="Flipped Horizontal", use_container_width=True)
112
 
113
+ elif filter_option == "Flip Vertical":
114
+ filtered_image = image.transpose(Image.FLIP_TOP_BOTTOM)
115
+ st.image(filtered_image, caption="Flipped Vertical", use_container_width=True)
116
 
117
+ elif filter_option == "Rotate 90°":
118
+ filtered_image = image.rotate(90)
119
+ st.image(filtered_image, caption="Rotated 90°", use_container_width=True)
120
 
121
+ else:
122
+ st.info("Select a filter to see output.")
123