Phani1008 commited on
Commit
2a6dd60
Β·
verified Β·
1 Parent(s): b3d27c9

Rename pages/Image_Augmentation_Handson.py to pages/Image_Augmentation.py

Browse files
pages/{Image_Augmentation_Handson.py β†’ Image_Augmentation.py} RENAMED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from PIL import Image, ImageOps
3
  import numpy as np
4
  import io
5
 
@@ -29,104 +29,85 @@ st.markdown(
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
- [
46
- "None",
47
- "Cropping",
48
- "Flipping",
49
- "Rotation",
50
- "Zoom In",
51
- "Zoom Out",
52
- "Translation",
53
- "Shearing",
54
- ],
55
  )
 
 
 
 
 
56
 
57
- augmented_image = image
58
-
59
- try:
60
- # Cropping
61
  if augmentation_option == "Cropping":
62
  left = st.slider("Left Crop", 0, image.width // 2, 0)
63
  top = st.slider("Top Crop", 0, image.height // 2, 0)
64
  right = st.slider("Right Crop", 0, image.width // 2, 0)
65
  bottom = st.slider("Bottom Crop", 0, image.height // 2, 0)
66
- augmented_image = image.crop((left, top, image.width - right, image.height - bottom))
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":
74
- augmented_image = ImageOps.flip(image)
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")
 
1
  import streamlit as st
2
+ from PIL import Image, ImageOps, ImageEnhance
3
  import numpy as np
4
  import io
5
 
 
29
  unsafe_allow_html=True,
30
  )
31
 
32
+ st.title("🌟 Image Augmentation Tool Hands-On Experience", anchor=False)
33
 
34
+ uploaded_image = st.sidebar.file_uploader("πŸ“€ Upload an Image", type=["jpg", "png", "jpeg"])
35
 
36
  if uploaded_image:
37
  try:
38
  image = Image.open(uploaded_image)
39
+ st.sidebar.image(image, caption="πŸ“Έ Uploaded Image", use_column_width=True)
40
 
41
+ st.sidebar.subheader("✨ Augmentation Options")
42
 
43
+ augmentation_options = st.sidebar.multiselect(
44
+ "Choose augmentations:",
45
+ ["Cropping", "Flipping", "Rotation", "Zoom In", "Zoom Out", "Translation", "Shearing"]
 
 
 
 
 
 
 
 
 
46
  )
47
+
48
+ augmented_image = image.copy()
49
+
50
+ # Live preview toggle
51
+ live_preview = st.sidebar.checkbox("πŸ”„ Live Preview", value=True)
52
 
53
+ for augmentation_option in augmentation_options:
 
 
 
54
  if augmentation_option == "Cropping":
55
  left = st.slider("Left Crop", 0, image.width // 2, 0)
56
  top = st.slider("Top Crop", 0, image.height // 2, 0)
57
  right = st.slider("Right Crop", 0, image.width // 2, 0)
58
  bottom = st.slider("Bottom Crop", 0, image.height // 2, 0)
59
+ augmented_image = augmented_image.crop((left, top, image.width - right, image.height - bottom))
60
 
 
61
  elif augmentation_option == "Flipping":
62
+ flip_option = st.selectbox("πŸ”„ Flip Type", ["Horizontal", "Vertical"], key=f"flip_{augmentation_option}")
63
  if flip_option == "Horizontal":
64
+ augmented_image = ImageOps.mirror(augmented_image)
65
  elif flip_option == "Vertical":
66
+ augmented_image = ImageOps.flip(augmented_image)
67
 
 
68
  elif augmentation_option == "Rotation":
69
  rotation_angle = st.slider("β€Ύ Rotation (Β°)", 0, 360, 0)
70
+ augmented_image = augmented_image.rotate(rotation_angle, expand=True)
71
 
 
72
  elif augmentation_option == "Zoom In":
73
  scale_factor = st.slider("πŸ” Zoom Factor (%)", 100, 200, 100) / 100.0
74
+ width, height = augmented_image.size
75
  new_width = int(width * scale_factor)
76
  new_height = int(height * scale_factor)
77
+ zoomed_image = augmented_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
  elif augmentation_option == "Zoom Out":
82
  scale_factor = st.slider("πŸ”Ž Zoom Out Factor (%)", 50, 100, 100) / 100.0
83
+ width, height = augmented_image.size
84
  new_width = int(width * scale_factor)
85
  new_height = int(height * scale_factor)
86
+ zoomed_image = augmented_image.resize((new_width, new_height))
87
  padded_image = Image.new("RGB", (width, height), (0, 0, 0))
88
  padded_image.paste(zoomed_image, ((width - new_width) // 2, (height - new_height) // 2))
89
  augmented_image = padded_image
90
 
 
91
  elif augmentation_option == "Translation":
92
  translation_x = st.slider("↔ Horizontal Shift (Pixels)", -100, 100, 0)
93
  translation_y = st.slider("↕ Vertical Shift (Pixels)", -100, 100, 0)
94
  translation_matrix = (1, 0, translation_x, 0, 1, translation_y)
95
+ augmented_image = augmented_image.transform(
96
+ augmented_image.size, Image.AFFINE, translation_matrix, fillcolor=(0, 0, 0)
97
  )
98
 
 
99
  elif augmentation_option == "Shearing":
100
  shear_angle = st.slider("πŸŒ€ Shearing Angle (Β°)", -45, 45, 0)
101
  shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
102
+ augmented_image = augmented_image.transform(
103
+ (augmented_image.width + abs(int(augmented_image.height * np.tan(np.radians(shear_angle)))), augmented_image.height),
104
  Image.AFFINE,
105
  shear_matrix,
106
  fillcolor=(0, 0, 0),
107
  )
108
+
 
 
109
  st.subheader("πŸ–ΌοΈ Augmented Image")
110
+ st.image(augmented_image, caption="Augmentation Applied", use_column_width=True)
111
 
112
  buffer = io.BytesIO()
113
  augmented_image.save(buffer, format="PNG")