Prathamesh1420 commited on
Commit
4fef606
·
verified ·
1 Parent(s): 5b44713

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -98
app.py CHANGED
@@ -5,116 +5,108 @@ import io
5
  # Set Streamlit page configuration
6
  st.set_page_config(page_title="Image Transformer", page_icon="🖼️", layout="wide")
7
 
 
 
 
 
 
 
 
 
8
  st.title("Image Manipulation with Generative AI")
9
- st.write("Select an option and upload an image to apply the transformation")
10
-
11
- # Create columns for layout
12
- col1, col2 = st.columns([1, 2])
13
-
14
- with col1:
15
- # Transformation options visible before image upload
16
- st.header("Transformation Options")
17
- operation = st.selectbox(
18
- "Choose an operation to perform:",
19
- ("Mirror", "Resize", "Crop", "Rotate", "Black/White", "Pixelate", "Compress")
20
- )
21
-
22
- # Show relevant sliders or options based on the chosen operation
23
- if operation == "Resize":
 
 
 
 
 
 
 
 
 
 
 
24
  new_width = st.slider("Resize Image - Width", 50, 500, 100)
25
  new_height = st.slider("Resize Image - Height", 50, 500, 100)
 
 
 
 
 
 
 
 
26
 
27
  elif operation == "Crop":
 
28
  crop_values = st.slider("Crop Image - (left, upper, right, lower)", 0, 100, (10, 10, 90, 90))
 
 
 
 
 
 
 
 
29
 
30
  elif operation == "Rotate":
 
31
  angle = st.slider("Rotate Image", 0, 360, 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  elif operation == "Pixelate":
 
34
  pixel_size = st.slider("Pixelate Image", 1, 20, 10)
 
 
 
 
 
 
 
 
 
35
 
36
  elif operation == "Compress":
 
37
  quality = st.slider("Compress Image (Quality)", 10, 100, 80)
38
-
39
- with col2:
40
- # Upload image
41
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
42
-
43
- if uploaded_file is not None:
44
- # Load image
45
- image = Image.open(uploaded_file)
46
-
47
- # Display smaller uploaded image
48
- st.image(image, caption="Original Image", use_column_width=False, width=250)
49
-
50
- # Apply transformation based on user selection
51
- if operation == "Mirror":
52
- if st.button("Apply Mirror"):
53
- mirrored_image = ImageOps.mirror(image)
54
- st.image(mirrored_image, caption="Mirrored Image", use_column_width=False, width=250)
55
-
56
- # Download button for mirrored image
57
- buffered = io.BytesIO()
58
- mirrored_image.save(buffered, format="PNG")
59
- st.download_button("Download Image", data=buffered.getvalue(), file_name="mirrored_image.png", mime="image/png")
60
-
61
- elif operation == "Resize":
62
- if st.button("Apply Resize"):
63
- resized_image = image.resize((new_width, new_height))
64
- st.image(resized_image, caption="Resized Image", use_column_width=False, width=250)
65
-
66
- # Download button for resized image
67
- buffered = io.BytesIO()
68
- resized_image.save(buffered, format="PNG")
69
- st.download_button("Download Image", data=buffered.getvalue(), file_name="resized_image.png", mime="image/png")
70
-
71
- elif operation == "Crop":
72
- if st.button("Apply Crop"):
73
- cropped_image = image.crop(crop_values)
74
- st.image(cropped_image, caption="Cropped Image", use_column_width=False, width=250)
75
-
76
- # Download button for cropped image
77
- buffered = io.BytesIO()
78
- cropped_image.save(buffered, format="PNG")
79
- st.download_button("Download Image", data=buffered.getvalue(), file_name="cropped_image.png", mime="image/png")
80
-
81
- elif operation == "Rotate":
82
- if st.button("Apply Rotate"):
83
- rotated_image = image.rotate(angle)
84
- st.image(rotated_image, caption="Rotated Image", use_column_width=False, width=250)
85
-
86
- # Download button for rotated image
87
- buffered = io.BytesIO()
88
- rotated_image.save(buffered, format="PNG")
89
- st.download_button("Download Image", data=buffered.getvalue(), file_name="rotated_image.png", mime="image/png")
90
-
91
- elif operation == "Black/White":
92
- if st.button("Apply Black/White"):
93
- bw_image = image.convert("L")
94
- st.image(bw_image, caption="Black & White Image", use_column_width=False, width=250)
95
-
96
- # Download button for black-and-white image
97
- buffered = io.BytesIO()
98
- bw_image.save(buffered, format="PNG")
99
- st.download_button("Download Image", data=buffered.getvalue(), file_name="bw_image.png", mime="image/png")
100
-
101
- elif operation == "Pixelate":
102
- if st.button("Apply Pixelate"):
103
- small = image.resize((image.width // pixel_size, image.height // pixel_size), Image.NEAREST)
104
- pixelated_image = small.resize((image.width, image.height), Image.NEAREST)
105
- st.image(pixelated_image, caption="Pixelated Image", use_column_width=False, width=250)
106
-
107
- # Download button for pixelated image
108
- buffered = io.BytesIO()
109
- pixelated_image.save(buffered, format="PNG")
110
- st.download_button("Download Image", data=buffered.getvalue(), file_name="pixelated_image.png", mime="image/png")
111
-
112
- elif operation == "Compress":
113
- if st.button("Apply Compress"):
114
- compressed_image = image.copy()
115
- st.image(compressed_image, caption=f"Compressed Image (Quality: {quality})", use_column_width=False, width=250)
116
-
117
- # Download button for compressed image
118
- buffered = io.BytesIO()
119
- compressed_image.save(buffered, format="PNG")
120
- st.download_button("Download Image", data=buffered.getvalue(), file_name="compressed_image.png", mime="image/png")
 
5
  # Set Streamlit page configuration
6
  st.set_page_config(page_title="Image Transformer", page_icon="🖼️", layout="wide")
7
 
8
+ # Sidebar for navigation and options
9
+ st.sidebar.title("Transformation Options")
10
+ operation = st.sidebar.selectbox(
11
+ "Choose an operation to perform:",
12
+ ("Mirror", "Resize", "Crop", "Rotate", "Black/White", "Pixelate", "Compress")
13
+ )
14
+
15
+ # Main title for the app
16
  st.title("Image Manipulation with Generative AI")
17
+ st.write("Upload an image and apply transformations based on your selection")
18
+
19
+ # Upload image
20
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
21
+
22
+ if uploaded_file is not None:
23
+ # Load the image
24
+ image = Image.open(uploaded_file)
25
+
26
+ # Show the original image (smaller size)
27
+ st.image(image, caption="Original Image", use_column_width=False, width=250)
28
+
29
+ # Apply transformations based on the selected operation
30
+ if operation == "Mirror":
31
+ st.header("Mirror Image")
32
+ if st.button("Apply Mirror"):
33
+ mirrored_image = ImageOps.mirror(image)
34
+ st.image(mirrored_image, caption="Mirrored Image", use_column_width=False, width=250)
35
+
36
+ # Download button for mirrored image
37
+ buffered = io.BytesIO()
38
+ mirrored_image.save(buffered, format="PNG")
39
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="mirrored_image.png", mime="image/png")
40
+
41
+ elif operation == "Resize":
42
+ st.header("Resize Image")
43
  new_width = st.slider("Resize Image - Width", 50, 500, 100)
44
  new_height = st.slider("Resize Image - Height", 50, 500, 100)
45
+ if st.button("Apply Resize"):
46
+ resized_image = image.resize((new_width, new_height))
47
+ st.image(resized_image, caption="Resized Image", use_column_width=False, width=250)
48
+
49
+ # Download button for resized image
50
+ buffered = io.BytesIO()
51
+ resized_image.save(buffered, format="PNG")
52
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="resized_image.png", mime="image/png")
53
 
54
  elif operation == "Crop":
55
+ st.header("Crop Image")
56
  crop_values = st.slider("Crop Image - (left, upper, right, lower)", 0, 100, (10, 10, 90, 90))
57
+ if st.button("Apply Crop"):
58
+ cropped_image = image.crop(crop_values)
59
+ st.image(cropped_image, caption="Cropped Image", use_column_width=False, width=250)
60
+
61
+ # Download button for cropped image
62
+ buffered = io.BytesIO()
63
+ cropped_image.save(buffered, format="PNG")
64
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="cropped_image.png", mime="image/png")
65
 
66
  elif operation == "Rotate":
67
+ st.header("Rotate Image")
68
  angle = st.slider("Rotate Image", 0, 360, 0)
69
+ if st.button("Apply Rotate"):
70
+ rotated_image = image.rotate(angle)
71
+ st.image(rotated_image, caption="Rotated Image", use_column_width=False, width=250)
72
+
73
+ # Download button for rotated image
74
+ buffered = io.BytesIO()
75
+ rotated_image.save(buffered, format="PNG")
76
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="rotated_image.png", mime="image/png")
77
+
78
+ elif operation == "Black/White":
79
+ st.header("Convert to Black/White")
80
+ if st.button("Apply Black/White"):
81
+ bw_image = image.convert("L")
82
+ st.image(bw_image, caption="Black & White Image", use_column_width=False, width=250)
83
+
84
+ # Download button for black-and-white image
85
+ buffered = io.BytesIO()
86
+ bw_image.save(buffered, format="PNG")
87
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="bw_image.png", mime="image/png")
88
 
89
  elif operation == "Pixelate":
90
+ st.header("Pixelate Image")
91
  pixel_size = st.slider("Pixelate Image", 1, 20, 10)
92
+ if st.button("Apply Pixelate"):
93
+ small = image.resize((image.width // pixel_size, image.height // pixel_size), Image.NEAREST)
94
+ pixelated_image = small.resize((image.width, image.height), Image.NEAREST)
95
+ st.image(pixelated_image, caption="Pixelated Image", use_column_width=False, width=250)
96
+
97
+ # Download button for pixelated image
98
+ buffered = io.BytesIO()
99
+ pixelated_image.save(buffered, format="PNG")
100
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="pixelated_image.png", mime="image/png")
101
 
102
  elif operation == "Compress":
103
+ st.header("Compress Image")
104
  quality = st.slider("Compress Image (Quality)", 10, 100, 80)
105
+ if st.button("Apply Compress"):
106
+ compressed_image = image.copy()
107
+ st.image(compressed_image, caption=f"Compressed Image (Quality: {quality})", use_column_width=False, width=250)
108
+
109
+ # Download button for compressed image
110
+ buffered = io.BytesIO()
111
+ compressed_image.save(buffered, format="PNG")
112
+ st.download_button("Download Image", data=buffered.getvalue(), file_name="compressed_image.png", mime="image/png")