Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,45 +12,46 @@ st.markdown("Upload an image and change its color dynamically!")
|
|
| 12 |
uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg", "webp"])
|
| 13 |
|
| 14 |
if uploaded_file is not None:
|
| 15 |
-
#
|
| 16 |
image = Image.open(uploaded_file)
|
| 17 |
image = np.array(image)
|
| 18 |
-
|
| 19 |
-
# Ensure the image has 3 channels (RGB)
|
| 20 |
-
if len(image.shape) == 2: # Grayscale image
|
| 21 |
-
|
| 22 |
-
elif image.shape[-1] == 4: # RGBA image
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
st.image(image, caption="Original Image", use_column_width=True)
|
| 26 |
-
|
| 27 |
# Color Picker
|
| 28 |
st.subheader("Select Target Color")
|
| 29 |
target_color = st.color_picker("Choose a color", "#ff0000") # Default red
|
| 30 |
-
|
| 31 |
# Convert Hex to RGB
|
| 32 |
hex_color = target_color.lstrip("#")
|
| 33 |
new_color = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
| 34 |
-
|
| 35 |
# Intensity Slider
|
| 36 |
intensity = st.slider("Color Intensity", 0, 100, 50)
|
| 37 |
-
|
| 38 |
-
# Change
|
| 39 |
def change_image_color(img, new_rgb, intensity=50):
|
| 40 |
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
| 41 |
img_hsv[:, :, 0] = new_rgb[0] * (intensity / 100) # Adjust Hue
|
| 42 |
img_hsv[:, :, 1] = new_rgb[1] * (intensity / 100) # Adjust Saturation
|
| 43 |
img_hsv[:, :, 2] = new_rgb[2] * (intensity / 100) # Adjust Brightness
|
| 44 |
return cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
|
| 45 |
-
|
| 46 |
# Apply Color Change
|
| 47 |
modified_image = change_image_color(image, new_color, intensity)
|
| 48 |
st.image(modified_image, caption="Modified Image", use_column_width=True)
|
| 49 |
-
|
| 50 |
# Download Button
|
| 51 |
img_byte_arr = io.BytesIO()
|
| 52 |
Image.fromarray(modified_image).save(img_byte_arr, format="PNG")
|
| 53 |
st.download_button("Download Modified Image", img_byte_arr.getvalue(), file_name="modified_image.png", mime="image/png")
|
| 54 |
-
|
| 55 |
st.markdown("---")
|
| 56 |
-
st.markdown("💡 Developed with ❤️ using Streamlit, OpenCV, and NumPy")
|
|
|
|
| 12 |
uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg", "webp"])
|
| 13 |
|
| 14 |
if uploaded_file is not None:
|
| 15 |
+
# Load image using PIL and convert to NumPy array
|
| 16 |
image = Image.open(uploaded_file)
|
| 17 |
image = np.array(image)
|
| 18 |
+
|
| 19 |
+
# Ensure the image has 3 channels (RGB)
|
| 20 |
+
if len(image.shape) == 2: # Grayscale image (Single Channel)
|
| 21 |
+
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 22 |
+
elif image.shape[-1] == 4: # RGBA image (4 channels)
|
| 23 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 24 |
+
|
| 25 |
+
# Display Original Image
|
| 26 |
st.image(image, caption="Original Image", use_column_width=True)
|
| 27 |
+
|
| 28 |
# Color Picker
|
| 29 |
st.subheader("Select Target Color")
|
| 30 |
target_color = st.color_picker("Choose a color", "#ff0000") # Default red
|
| 31 |
+
|
| 32 |
# Convert Hex to RGB
|
| 33 |
hex_color = target_color.lstrip("#")
|
| 34 |
new_color = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
| 35 |
+
|
| 36 |
# Intensity Slider
|
| 37 |
intensity = st.slider("Color Intensity", 0, 100, 50)
|
| 38 |
+
|
| 39 |
+
# Function to Change Image Color
|
| 40 |
def change_image_color(img, new_rgb, intensity=50):
|
| 41 |
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
| 42 |
img_hsv[:, :, 0] = new_rgb[0] * (intensity / 100) # Adjust Hue
|
| 43 |
img_hsv[:, :, 1] = new_rgb[1] * (intensity / 100) # Adjust Saturation
|
| 44 |
img_hsv[:, :, 2] = new_rgb[2] * (intensity / 100) # Adjust Brightness
|
| 45 |
return cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
|
| 46 |
+
|
| 47 |
# Apply Color Change
|
| 48 |
modified_image = change_image_color(image, new_color, intensity)
|
| 49 |
st.image(modified_image, caption="Modified Image", use_column_width=True)
|
| 50 |
+
|
| 51 |
# Download Button
|
| 52 |
img_byte_arr = io.BytesIO()
|
| 53 |
Image.fromarray(modified_image).save(img_byte_arr, format="PNG")
|
| 54 |
st.download_button("Download Modified Image", img_byte_arr.getvalue(), file_name="modified_image.png", mime="image/png")
|
| 55 |
+
|
| 56 |
st.markdown("---")
|
| 57 |
+
st.markdown("💡 Developed with ❤️ using Streamlit, OpenCV, and NumPy")
|