Ramyamaheswari commited on
Commit
1f0c2d7
Β·
verified Β·
1 Parent(s): 07291c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -57
app.py CHANGED
@@ -4,85 +4,90 @@ import numpy as np
4
  from PIL import Image
5
  import io
6
 
7
- st.set_page_config(page_title="Image Transformer v3", layout="centered")
8
- st.title("πŸ§ͺ Advanced Image Transformer")
9
 
10
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
11
 
12
  if uploaded_file:
13
  try:
14
  image = Image.open(uploaded_file).convert("RGB")
15
  img_array = np.array(image)
16
- transformed = img_array.copy()
 
 
 
17
 
18
- st.markdown("### πŸ”§ Transform Settings")
19
- with st.expander("🎨 Effects"):
20
- effect = st.selectbox("Select an effect", ["None", "Grayscale", "Blur", "Edges", "HSV", "LAB"])
21
 
22
- with st.expander("πŸŒ€ Rotation and Flip"):
23
- angle = st.slider("Rotation angle", -180, 180, 0)
24
- flip = st.radio("Flip", ["None", "Horizontal", "Vertical"])
25
 
26
- with st.expander("πŸ“ Translation"):
27
- dx = st.slider("Translate X", -100, 100, 0)
28
- dy = st.slider("Translate Y", -100, 100, 0)
29
 
30
- with st.expander("βœ‚οΈ Crop"):
31
- enable_crop = st.checkbox("Enable Crop")
32
- if enable_crop:
33
- h, w = img_array.shape[:2]
34
- crop_top = st.slider("Top", 0, h // 2, 0)
35
- crop_bottom = st.slider("Bottom", 0, h // 2, 0)
36
- crop_left = st.slider("Left", 0, w // 2, 0)
37
- crop_right = st.slider("Right", 0, w // 2, 0)
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Apply Effect
40
  if effect == "Grayscale":
41
  transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
42
- elif effect == "Blur":
43
- transformed = cv2.GaussianBlur(transformed, (9, 9), 0)
44
- elif effect == "Edges":
45
  gray = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
46
- transformed = cv2.Canny(gray, 50, 150)
47
  elif effect == "HSV":
48
  transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2HSV)
49
  elif effect == "LAB":
50
  transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2Lab)
51
 
52
- # Flip
53
- if flip == "Horizontal":
 
 
 
 
 
 
54
  transformed = cv2.flip(transformed, 1)
55
- elif flip == "Vertical":
56
  transformed = cv2.flip(transformed, 0)
57
 
58
- # Rotate
59
- rows, cols = transformed.shape[:2]
60
- M_rot = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
61
- transformed = cv2.warpAffine(transformed, M_rot, (cols, rows))
62
-
63
- # Translate
64
- M_trans = np.float32([[1, 0, dx], [0, 1, dy]])
65
- transformed = cv2.warpAffine(transformed, M_trans, (cols, rows))
66
-
67
- # Crop
68
- if enable_crop:
69
- h, w = transformed.shape[:2]
70
- transformed = transformed[crop_top:h - crop_bottom, crop_left:w - crop_right]
71
-
72
- # Show in tabs
73
- tab1, tab2 = st.tabs(["πŸ–ΌοΈ Original", "🎨 Transformed"])
74
- with tab1:
75
- st.image(img_array, caption="Original", use_column_width=True)
76
- with tab2:
77
- st.image(transformed, caption="Transformed", use_column_width=True, channels="GRAY" if len(transformed.shape) == 2 else "RGB")
78
-
79
- # Download
80
- final_img = Image.fromarray(cv2.cvtColor(transformed, cv2.COLOR_GRAY2RGB) if len(transformed.shape) == 2 else transformed)
81
- buffer = io.BytesIO()
82
- final_img.save(buffer, format="PNG")
83
- st.download_button("πŸ“₯ Download Result", buffer.getvalue(), "transformed.png", "image/png")
84
 
85
  except Exception as e:
86
- st.error(f"Something went wrong: {e}")
 
87
  else:
88
- st.info("Please upload an image to get started.")
 
4
  from PIL import Image
5
  import io
6
 
7
+ st.set_page_config(page_title="Image Transformer", layout="centered")
8
+ st.title("πŸ–ΌοΈ Image Transformer")
9
 
10
+ uploaded_file = st.file_uploader("πŸ“€ Upload an Image", type=["jpg", "jpeg", "png"])
11
 
12
  if uploaded_file:
13
  try:
14
  image = Image.open(uploaded_file).convert("RGB")
15
  img_array = np.array(image)
16
+ original_shape = img_array.shape
17
+
18
+ # Show original in smaller size
19
+ st.image(img_array, caption=f"Original Image | {original_shape[1]}x{original_shape[0]}", width=300)
20
 
21
+ # Sidebar controls
22
+ with st.sidebar.expander("🎨 Effects"):
23
+ effect = st.selectbox("Choose Effect", ["None", "Grayscale", "Gaussian Blur", "Canny Edge Detection", "HSV", "LAB"])
24
 
25
+ with st.sidebar.expander("πŸ”„ Rotate"):
26
+ rotate = st.slider("Rotate (degrees)", -180, 180, 0)
 
27
 
28
+ with st.sidebar.expander("↔️ Flip"):
29
+ flip_h = st.checkbox("Flip Horizontally")
30
+ flip_v = st.checkbox("Flip Vertically")
31
 
32
+ with st.sidebar.expander("πŸ“ Shear & Translate"):
33
+ shear_x = st.slider("Shear X", -0.5, 0.5, 0.0)
34
+ shear_y = st.slider("Shear Y", -0.5, 0.5, 0.0)
35
+ translate_x = st.slider("Translate X", -100, 100, 0)
36
+ translate_y = st.slider("Translate Y", -100, 100, 0)
37
+
38
+ with st.sidebar.expander("βœ‚οΈ Crop"):
39
+ crop = st.checkbox("Enable Cropping")
40
+ if crop:
41
+ max_top = original_shape[0] // 2
42
+ max_side = original_shape[1] // 2
43
+ crop_top = st.slider("Crop Top", 0, max_top, 0)
44
+ crop_bottom = st.slider("Crop Bottom", 0, max_top, 0)
45
+ crop_left = st.slider("Crop Left", 0, max_side, 0)
46
+ crop_right = st.slider("Crop Right", 0, max_side, 0)
47
+
48
+ # Start transformation
49
+ transformed = img_array.copy()
50
 
 
51
  if effect == "Grayscale":
52
  transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
53
+ elif effect == "Gaussian Blur":
54
+ transformed = cv2.GaussianBlur(transformed, (11, 11), 0)
55
+ elif effect == "Canny Edge Detection":
56
  gray = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
57
+ transformed = cv2.Canny(gray, 100, 200)
58
  elif effect == "HSV":
59
  transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2HSV)
60
  elif effect == "LAB":
61
  transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2Lab)
62
 
63
+ rows, cols = transformed.shape[:2]
64
+ M = np.float32([[1, shear_x, translate_x], [shear_y, 1, translate_y]])
65
+ transformed = cv2.warpAffine(transformed, M, (cols, rows))
66
+
67
+ M_rotate = cv2.getRotationMatrix2D((cols / 2, rows / 2), rotate, 1)
68
+ transformed = cv2.warpAffine(transformed, M_rotate, (cols, rows))
69
+
70
+ if flip_h:
71
  transformed = cv2.flip(transformed, 1)
72
+ if flip_v:
73
  transformed = cv2.flip(transformed, 0)
74
 
75
+ if crop:
76
+ transformed = transformed[crop_top:rows - crop_bottom, crop_left:cols - crop_right]
77
+
78
+ # Show transformed in smaller size
79
+ st.image(transformed, caption="Transformed Image", width=300, channels="GRAY" if len(transformed.shape) == 2 else "RGB")
80
+
81
+ # Download button
82
+ final_image = Image.fromarray(cv2.cvtColor(transformed, cv2.COLOR_GRAY2RGB) if len(transformed.shape) == 2 else transformed)
83
+ buf = io.BytesIO()
84
+ final_image.save(buf, format="PNG")
85
+ byte_im = buf.getvalue()
86
+
87
+ st.download_button("πŸ“₯ Download Transformed Image", byte_im, "transformed.png", "image/png")
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  except Exception as e:
90
+ st.error(f"❌ Error: {e}")
91
+
92
  else:
93
+ st.info("πŸ“Œ Upload an image to get started.")