Prathamesh1420 commited on
Commit
4eb73d8
1 Parent(s): eaa9a88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -13
app.py CHANGED
@@ -33,8 +33,36 @@ if uploaded_file is not None:
33
  with col1:
34
  st.image(image, caption="Original Image", use_column_width=False, width=250)
35
 
36
- # Apply transformations based on the user selection
37
- if col2.button("Apply", key=operation): # Show button between images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  with col3:
39
  # Mirror Operation
40
  if operation == "Mirror":
@@ -47,9 +75,7 @@ if uploaded_file is not None:
47
  st.download_button("Download Image", data=buffered.getvalue(), file_name="mirrored_image.png", mime="image/png")
48
 
49
  # Resize Operation
50
- elif operation == "Resize":
51
- new_width = st.slider("Resize Image - Width", 50, 500, 100)
52
- new_height = st.slider("Resize Image - Height", 50, 500, 100)
53
  resized_image = image.resize((new_width, new_height))
54
  st.image(resized_image, caption=" ", use_column_width=False, width=250)
55
 
@@ -59,8 +85,7 @@ if uploaded_file is not None:
59
  st.download_button("Download Image", data=buffered.getvalue(), file_name="resized_image.png", mime="image/png")
60
 
61
  # Crop Operation
62
- elif operation == "Crop":
63
- crop_values = st.slider("Crop Image - (left, upper, right, lower)", 0, 100, (10, 10, 90, 90))
64
  cropped_image = image.crop(crop_values)
65
  st.image(cropped_image, caption=" ", use_column_width=False, width=250)
66
 
@@ -70,8 +95,7 @@ if uploaded_file is not None:
70
  st.download_button("Download Image", data=buffered.getvalue(), file_name="cropped_image.png", mime="image/png")
71
 
72
  # Rotate Operation
73
- elif operation == "Rotate":
74
- angle = st.slider("Rotate Image", 0, 360, 0)
75
  rotated_image = image.rotate(angle)
76
  st.image(rotated_image, caption=" ", use_column_width=False, width=250)
77
 
@@ -91,8 +115,7 @@ if uploaded_file is not None:
91
  st.download_button("Download Image", data=buffered.getvalue(), file_name="bw_image.png", mime="image/png")
92
 
93
  # Pixelate Operation
94
- elif operation == "Pixelate":
95
- pixel_size = st.slider("Pixelate Image", 1, 20, 10)
96
  small = image.resize((image.width // pixel_size, image.height // pixel_size), Image.NEAREST)
97
  pixelated_image = small.resize((image.width, image.height), Image.NEAREST)
98
  st.image(pixelated_image, caption=" ", use_column_width=False, width=250)
@@ -103,8 +126,7 @@ if uploaded_file is not None:
103
  st.download_button("Download Image", data=buffered.getvalue(), file_name="pixelated_image.png", mime="image/png")
104
 
105
  # Compress Operation
106
- elif operation == "Compress":
107
- quality = st.slider("Compress Image (Quality)", 10, 100, 80)
108
  compressed_image = image.copy() # In real-world, this should apply compression logic
109
  st.image(compressed_image, caption=" ", use_column_width=False, width=250)
110
 
 
33
  with col1:
34
  st.image(image, caption="Original Image", use_column_width=False, width=250)
35
 
36
+ # Variables to store slider values
37
+ new_width = None
38
+ new_height = None
39
+ crop_values = None
40
+ angle = None
41
+ pixel_size = None
42
+ quality = None
43
+
44
+ # Display sliders based on operation, but only apply when the button is pressed
45
+ if operation == "Resize":
46
+ new_width = st.slider("Resize Image - Width", 50, 500, 100)
47
+ new_height = st.slider("Resize Image - Height", 50, 500, 100)
48
+
49
+ elif operation == "Crop":
50
+ crop_values = st.slider("Crop Image - (left, upper, right, lower)", 0, 100, (10, 10, 90, 90))
51
+
52
+ elif operation == "Rotate":
53
+ angle = st.slider("Rotate Image", 0, 360, 0)
54
+
55
+ elif operation == "Pixelate":
56
+ pixel_size = st.slider("Pixelate Image", 1, 20, 10)
57
+
58
+ elif operation == "Compress":
59
+ quality = st.slider("Compress Image (Quality)", 10, 100, 80)
60
+
61
+ # Apply button with dynamic label based on the operation
62
+ apply_button_label = f"Apply {operation}"
63
+
64
+ if col2.button(apply_button_label):
65
+ # Only generate the output when the "Apply" button is clicked
66
  with col3:
67
  # Mirror Operation
68
  if operation == "Mirror":
 
75
  st.download_button("Download Image", data=buffered.getvalue(), file_name="mirrored_image.png", mime="image/png")
76
 
77
  # Resize Operation
78
+ elif operation == "Resize" and new_width and new_height:
 
 
79
  resized_image = image.resize((new_width, new_height))
80
  st.image(resized_image, caption=" ", use_column_width=False, width=250)
81
 
 
85
  st.download_button("Download Image", data=buffered.getvalue(), file_name="resized_image.png", mime="image/png")
86
 
87
  # Crop Operation
88
+ elif operation == "Crop" and crop_values:
 
89
  cropped_image = image.crop(crop_values)
90
  st.image(cropped_image, caption=" ", use_column_width=False, width=250)
91
 
 
95
  st.download_button("Download Image", data=buffered.getvalue(), file_name="cropped_image.png", mime="image/png")
96
 
97
  # Rotate Operation
98
+ elif operation == "Rotate" and angle is not None:
 
99
  rotated_image = image.rotate(angle)
100
  st.image(rotated_image, caption=" ", use_column_width=False, width=250)
101
 
 
115
  st.download_button("Download Image", data=buffered.getvalue(), file_name="bw_image.png", mime="image/png")
116
 
117
  # Pixelate Operation
118
+ elif operation == "Pixelate" and pixel_size:
 
119
  small = image.resize((image.width // pixel_size, image.height // pixel_size), Image.NEAREST)
120
  pixelated_image = small.resize((image.width, image.height), Image.NEAREST)
121
  st.image(pixelated_image, caption=" ", use_column_width=False, width=250)
 
126
  st.download_button("Download Image", data=buffered.getvalue(), file_name="pixelated_image.png", mime="image/png")
127
 
128
  # Compress Operation
129
+ elif operation == "Compress" and quality:
 
130
  compressed_image = image.copy() # In real-world, this should apply compression logic
131
  st.image(compressed_image, caption=" ", use_column_width=False, width=250)
132