Phani1008 commited on
Commit
b3d27c9
·
verified ·
1 Parent(s): e6e493d

Update pages/Image_Augmentation_Handson.py

Browse files
Files changed (1) hide show
  1. pages/Image_Augmentation_Handson.py +69 -62
pages/Image_Augmentation_Handson.py CHANGED
@@ -3,31 +3,43 @@ from PIL import Image, ImageOps
3
  import numpy as np
4
  import io
5
 
6
- # Set Background color for the app while uploading an image
7
  st.markdown(
8
  """
9
  <style>
10
  .stApp {
11
- background-color: #000000;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
  </style>
14
  """,
15
  unsafe_allow_html=True,
16
  )
17
 
18
- st.title("Image Data Augmentation")
19
 
20
- # Image uploader
21
- uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
22
 
23
  if uploaded_image:
24
  try:
25
  image = Image.open(uploaded_image)
26
- st.image(image, caption="Uploaded Image", use_container_width=True)
27
-
28
- st.subheader("Select Augmentation Options")
29
 
30
- # Augmentation option
 
31
  augmentation_option = st.radio(
32
  "Choose an augmentation:",
33
  [
@@ -35,8 +47,8 @@ if uploaded_image:
35
  "Cropping",
36
  "Flipping",
37
  "Rotation",
38
- "Zooming In",
39
- "Zooming Out",
40
  "Translation",
41
  "Shearing",
42
  ],
@@ -55,7 +67,7 @@ if uploaded_image:
55
 
56
  # Flipping
57
  elif augmentation_option == "Flipping":
58
- flip_option = st.selectbox("Flip Type", ["Horizontal", "Vertical"])
59
  if flip_option == "Horizontal":
60
  augmented_image = ImageOps.mirror(image)
61
  elif flip_option == "Vertical":
@@ -63,68 +75,63 @@ if uploaded_image:
63
 
64
  # Rotation
65
  elif augmentation_option == "Rotation":
66
- rotation_angle = st.slider("Rotation (°)", 0, 360, 0)
67
- if rotation_angle != 0:
68
- augmented_image = image.rotate(rotation_angle, expand=True)
69
-
70
- # Zooming In
71
- elif augmentation_option == "Zooming In":
72
- scale_factor = st.slider("Zoom Factor (%)", 100, 200, 100) / 100.0
73
- if scale_factor != 1.0:
74
- width, height = image.size
75
- new_width = int(width * scale_factor)
76
- new_height = int(height * scale_factor)
77
- zoomed_image = image.resize((new_width, new_height))
78
- augmented_image = zoomed_image.crop(((new_width - width) // 2, (new_height - height) // 2,
79
- (new_width + width) // 2, (new_height + height) // 2))
80
-
81
- # Zooming Out
82
- elif augmentation_option == "Zooming Out":
83
- scale_factor = st.slider("Zoom Factor (%)", 50, 100, 100) / 100.0
84
- if scale_factor != 1.0:
85
- width, height = image.size
86
- new_width = int(width * scale_factor)
87
- new_height = int(height * scale_factor)
88
- zoomed_image = image.resize((new_width, new_height))
89
- padded_image = Image.new("RGB", (width, height), (255, 255, 255))
90
- padded_image.paste(zoomed_image, ((width - new_width) // 2, (height - new_height) // 2))
91
- augmented_image = padded_image
 
92
 
93
  # Translation
94
  elif augmentation_option == "Translation":
95
- translation_x = st.slider("Horizontal Shift (Pixels)", -100, 100, 0)
96
- translation_y = st.slider("Vertical Shift (Pixels)", -100, 100, 0)
97
- if translation_x != 0 or translation_y != 0:
98
- translation_matrix = (1, 0, translation_x, 0, 1, translation_y)
99
- augmented_image = image.transform(
100
- image.size, Image.AFFINE, translation_matrix, fillcolor=(255, 255, 255)
101
- )
102
 
103
  # Shearing
104
  elif augmentation_option == "Shearing":
105
- shear_angle = st.slider("Shearing Angle (°)", -45, 45, 0)
106
- if shear_angle != 0:
107
- shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
108
- augmented_image = image.transform(
109
- (image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
110
- Image.AFFINE,
111
- shear_matrix,
112
- fillcolor=(255, 255, 255),
113
- )
114
  except Exception as aug_error:
115
- st.error(f"Error during augmentation: {aug_error}")
116
 
117
- # Display augmented image
118
- st.subheader("Augmented Image")
119
  st.image(augmented_image, caption=f"{augmentation_option} Applied", use_container_width=True)
120
 
121
- # Download augmented image
122
  buffer = io.BytesIO()
123
  augmented_image.save(buffer, format="PNG")
124
  buffer.seek(0)
125
- st.download_button(
126
- "Download Augmented Image🎃", buffer, file_name="augmented_image.png"
127
- )
128
 
129
  except Exception as e:
130
- st.error(f"An error occurred: {e}")
 
3
  import numpy as np
4
  import io
5
 
6
+ # Custom CSS for enhanced styling
7
  st.markdown(
8
  """
9
  <style>
10
  .stApp {
11
+ background-color: #121212;
12
+ color: #E0E0E0;
13
+ }
14
+ .stTitle {
15
+ text-align: center;
16
+ color: #4CAF50;
17
+ }
18
+ .css-1d391kg p, .css-1v0mbdj p {
19
+ color: #B0BEC5;
20
+ }
21
+ .css-1aumxhk .stSlider {
22
+ background-color: #4CAF50;
23
+ }
24
+ .css-2trqyj, .css-16idsys p {
25
+ color: #BB86FC;
26
  }
27
  </style>
28
  """,
29
  unsafe_allow_html=True,
30
  )
31
 
32
+ st.title("🌟 Enhanced Image Augmentation Tool", anchor=False)
33
 
34
+ uploaded_image = st.file_uploader("📤 Upload an Image", type=["jpg", "png", "jpeg"])
 
35
 
36
  if uploaded_image:
37
  try:
38
  image = Image.open(uploaded_image)
39
+ st.image(image, caption="📸 Uploaded Image", use_container_width=True)
 
 
40
 
41
+ st.subheader("✨ Select Augmentation Options")
42
+
43
  augmentation_option = st.radio(
44
  "Choose an augmentation:",
45
  [
 
47
  "Cropping",
48
  "Flipping",
49
  "Rotation",
50
+ "Zoom In",
51
+ "Zoom Out",
52
  "Translation",
53
  "Shearing",
54
  ],
 
67
 
68
  # Flipping
69
  elif augmentation_option == "Flipping":
70
+ flip_option = st.selectbox("🔄 Flip Type", ["Horizontal", "Vertical"])
71
  if flip_option == "Horizontal":
72
  augmented_image = ImageOps.mirror(image)
73
  elif flip_option == "Vertical":
 
75
 
76
  # Rotation
77
  elif augmentation_option == "Rotation":
78
+ rotation_angle = st.slider("Rotation (°)", 0, 360, 0)
79
+ augmented_image = image.rotate(rotation_angle, expand=True)
80
+
81
+ # Zoom In
82
+ elif augmentation_option == "Zoom In":
83
+ scale_factor = st.slider("🔍 Zoom Factor (%)", 100, 200, 100) / 100.0
84
+ width, height = image.size
85
+ new_width = int(width * scale_factor)
86
+ new_height = int(height * scale_factor)
87
+ zoomed_image = image.resize((new_width, new_height))
88
+ augmented_image = zoomed_image.crop((
89
+ (new_width - width) // 2,
90
+ (new_height - height) // 2,
91
+ (new_width + width) // 2,
92
+ (new_height + height) // 2,
93
+ ))
94
+
95
+ # Zoom Out
96
+ elif augmentation_option == "Zoom Out":
97
+ scale_factor = st.slider("🔎 Zoom Out Factor (%)", 50, 100, 100) / 100.0
98
+ width, height = image.size
99
+ new_width = int(width * scale_factor)
100
+ new_height = int(height * scale_factor)
101
+ zoomed_image = image.resize((new_width, new_height))
102
+ padded_image = Image.new("RGB", (width, height), (0, 0, 0))
103
+ padded_image.paste(zoomed_image, ((width - new_width) // 2, (height - new_height) // 2))
104
+ augmented_image = padded_image
105
 
106
  # Translation
107
  elif augmentation_option == "Translation":
108
+ translation_x = st.slider("Horizontal Shift (Pixels)", -100, 100, 0)
109
+ translation_y = st.slider("Vertical Shift (Pixels)", -100, 100, 0)
110
+ translation_matrix = (1, 0, translation_x, 0, 1, translation_y)
111
+ augmented_image = image.transform(
112
+ image.size, Image.AFFINE, translation_matrix, fillcolor=(0, 0, 0)
113
+ )
 
114
 
115
  # Shearing
116
  elif augmentation_option == "Shearing":
117
+ shear_angle = st.slider("🌀 Shearing Angle (°)", -45, 45, 0)
118
+ shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
119
+ augmented_image = image.transform(
120
+ (image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
121
+ Image.AFFINE,
122
+ shear_matrix,
123
+ fillcolor=(0, 0, 0),
124
+ )
 
125
  except Exception as aug_error:
126
+ st.error(f"⚠️ Augmentation Error: {aug_error}")
127
 
128
+ st.subheader("🖼️ Augmented Image")
 
129
  st.image(augmented_image, caption=f"{augmentation_option} Applied", use_container_width=True)
130
 
 
131
  buffer = io.BytesIO()
132
  augmented_image.save(buffer, format="PNG")
133
  buffer.seek(0)
134
+ st.download_button("📥 Download Augmented Image", buffer, file_name="augmented_image.png")
 
 
135
 
136
  except Exception as e:
137
+ st.error(f"An error occurred: {e}")