pratyyush commited on
Commit
b7721f7
·
verified ·
1 Parent(s): 292a1fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -38
app.py CHANGED
@@ -3,9 +3,8 @@ from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
3
  import io
4
  import numpy as np
5
  import cv2
6
- from rembg import remove # For background removal
7
 
8
- # ✅ Add custom CSS for black background and consistent layout
9
  st.markdown(
10
  """
11
  <style>
@@ -43,22 +42,10 @@ st.markdown(
43
  .stDownloadButton>button:hover {
44
  background-color: #007bb5;
45
  }
46
- /* Flex container for side-by-side images */
47
- .image-container {
48
- display: flex;
49
- justify-content: space-around;
50
- align-items: center;
51
- }
52
- .image-box {
53
- max-width: 45%;
54
- margin: 10px;
55
- text-align: center;
56
- }
57
  img {
58
- width: 100%;
59
- height: auto;
60
- border-radius: 10px;
61
- box-shadow: 0 4px 8px rgba(0,0,0,0.2);
62
  }
63
  </style>
64
  """,
@@ -73,9 +60,9 @@ st.write("Enhance, edit, and apply AI-powered enhancements to your images!")
73
  uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
74
 
75
  if uploaded_file:
76
- # Load original image
77
  image = Image.open(uploaded_file)
78
- original_image = image.copy()
79
 
80
  # Sidebar Tools
81
  st.sidebar.title("Editing Tools")
@@ -117,18 +104,20 @@ if uploaded_file:
117
  sketch = cv2.divide(gray, 255 - blur, scale=256)
118
  image = Image.fromarray(sketch)
119
 
120
- # Background Removal
121
- if st.sidebar.checkbox("Remove Background"):
122
- image = Image.open(io.BytesIO(remove(uploaded_file.read())))
123
-
124
  # Watermarking
125
  if st.sidebar.checkbox("Add Watermark"):
126
  watermark_text = st.sidebar.text_input("Watermark Text", "My Watermark")
127
  draw = ImageDraw.Draw(image)
128
- font = ImageFont.load_default() # Use default font if arial.ttf doesn't work
 
 
 
 
 
 
129
  width, height = image.size
130
  text_width, text_height = draw.textsize(watermark_text, font)
131
- x, y = width - text_width - 10, height - text_height - 10
132
  draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255, 128))
133
 
134
  # Enhance Image
@@ -145,21 +134,17 @@ if uploaded_file:
145
  except Exception as e:
146
  st.write(f"Error during enhancement: {e}")
147
 
148
- # Display Images Side by Side
149
- st.markdown('<div class="image-container">', unsafe_allow_html=True)
150
-
151
- col1, col2 = st.columns(2)
152
-
153
- with col1:
154
- st.image(original_image, caption="Original Image", use_column_width=True)
155
-
156
- with col2:
157
- st.image(image, caption="Enhanced Image", use_column_width=True)
158
-
159
- st.markdown('</div>', unsafe_allow_html=True)
160
 
161
  # Download Button
162
  buf = io.BytesIO()
163
  image.save(buf, format="PNG")
164
  byte_im = buf.getvalue()
165
- st.download_button("Download Enhanced Image", data=byte_im, file_name="enhanced_image.png", mime="image/png")
 
 
 
 
 
 
 
3
  import io
4
  import numpy as np
5
  import cv2
 
6
 
7
+ # ✅ Add custom CSS for black background and non-draggable images
8
  st.markdown(
9
  """
10
  <style>
 
42
  .stDownloadButton>button:hover {
43
  background-color: #007bb5;
44
  }
45
+ /* Make images non-draggable */
 
 
 
 
 
 
 
 
 
 
46
  img {
47
+ pointer-events: none;
48
+ -webkit-user-drag: none;
 
 
49
  }
50
  </style>
51
  """,
 
60
  uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
61
 
62
  if uploaded_file:
63
+ # Load image
64
  image = Image.open(uploaded_file)
65
+ st.image(image, caption="Uploaded Image", use_column_width=True)
66
 
67
  # Sidebar Tools
68
  st.sidebar.title("Editing Tools")
 
104
  sketch = cv2.divide(gray, 255 - blur, scale=256)
105
  image = Image.fromarray(sketch)
106
 
 
 
 
 
107
  # Watermarking
108
  if st.sidebar.checkbox("Add Watermark"):
109
  watermark_text = st.sidebar.text_input("Watermark Text", "My Watermark")
110
  draw = ImageDraw.Draw(image)
111
+
112
+ # Try loading arial font, fallback to default
113
+ try:
114
+ font = ImageFont.truetype("arial.ttf", 30)
115
+ except IOError:
116
+ font = ImageFont.load_default()
117
+
118
  width, height = image.size
119
  text_width, text_height = draw.textsize(watermark_text, font)
120
+ x, y = width - text_width - 20, height - text_height - 20
121
  draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255, 128))
122
 
123
  # Enhance Image
 
134
  except Exception as e:
135
  st.write(f"Error during enhancement: {e}")
136
 
137
+ # Display Enhanced Image
138
+ st.image(image, caption="Enhanced Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
139
 
140
  # Download Button
141
  buf = io.BytesIO()
142
  image.save(buf, format="PNG")
143
  byte_im = buf.getvalue()
144
+
145
+ st.download_button(
146
+ "Download Enhanced Image",
147
+ data=byte_im,
148
+ file_name="enhanced_image.png",
149
+ mime="image/png"
150
+ )