Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import io
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
def color_transfer(source, target):
|
| 8 |
+
# Convert images to LAB color space
|
| 9 |
+
source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB).astype("float32")
|
| 10 |
+
target = cv2.cvtColor(target, cv2.COLOR_BGR2LAB).astype("float32")
|
| 11 |
+
|
| 12 |
+
# Compute mean and std for each channel
|
| 13 |
+
mean_src, std_src = cv2.meanStdDev(source)
|
| 14 |
+
mean_tgt, std_tgt = cv2.meanStdDev(target)
|
| 15 |
+
|
| 16 |
+
# Reshape for broadcasting
|
| 17 |
+
mean_src, std_src = mean_src.reshape((1, 1, 3)), std_src.reshape((1, 1, 3))
|
| 18 |
+
mean_tgt, std_tgt = mean_tgt.reshape((1, 1, 3)), std_tgt.reshape((1, 1, 3))
|
| 19 |
+
|
| 20 |
+
# Apply color transfer
|
| 21 |
+
adjusted = ((target - mean_tgt) * (std_src / std_tgt)) + mean_src
|
| 22 |
+
adjusted = np.clip(adjusted, 0, 255).astype("uint8")
|
| 23 |
+
|
| 24 |
+
return cv2.cvtColor(adjusted, cv2.COLOR_LAB2BGR)
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
st.set_page_config(layout="wide")
|
| 28 |
+
st.sidebar.header("Upload Images")
|
| 29 |
+
|
| 30 |
+
# Upload two images in the sidebar
|
| 31 |
+
uploaded_file1 = st.sidebar.file_uploader("Upload first image", type=["jpg", "png", "jpeg"])
|
| 32 |
+
uploaded_file2 = st.sidebar.file_uploader("Upload second image", type=["jpg", "png", "jpeg"])
|
| 33 |
+
|
| 34 |
+
st.sidebar.image("test.png", width=700)
|
| 35 |
+
|
| 36 |
+
# Display the images in the center
|
| 37 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 38 |
+
|
| 39 |
+
with col1:
|
| 40 |
+
st.header("Uploaded Images")
|
| 41 |
+
|
| 42 |
+
if uploaded_file1 and uploaded_file2:
|
| 43 |
+
ref_img = Image.open(uploaded_file1)
|
| 44 |
+
input_img = Image.open(uploaded_file2)
|
| 45 |
+
|
| 46 |
+
# Display images side by side
|
| 47 |
+
st.image([ref_img, input_img], caption=["Reference Image", "Input Image"], width=300)
|
| 48 |
+
elif uploaded_file1:
|
| 49 |
+
ref_img = Image.open(uploaded_file1)
|
| 50 |
+
st.image(ref_img, caption="Reference Image", width=300)
|
| 51 |
+
elif uploaded_file2:
|
| 52 |
+
input_img = Image.open(uploaded_file2)
|
| 53 |
+
st.image(input_img, caption="Input Image", width=300)
|
| 54 |
+
else:
|
| 55 |
+
st.write("Upload images to display them here.")
|
| 56 |
+
|
| 57 |
+
with col2:
|
| 58 |
+
st.header(" ")
|
| 59 |
+
if uploaded_file1 and uploaded_file2:
|
| 60 |
+
img1 = Image.open(uploaded_file1)
|
| 61 |
+
img2 = Image.open(uploaded_file2)
|
| 62 |
+
|
| 63 |
+
ref_img = cv2.cvtColor(np.array(img1), cv2.COLOR_RGB2BGR)
|
| 64 |
+
input_img = cv2.cvtColor(np.array(img2), cv2.COLOR_RGB2BGR)
|
| 65 |
+
|
| 66 |
+
orig_h, orig_w = input_img.shape[:2]
|
| 67 |
+
input_img = cv2.resize(input_img, (ref_img.shape[1], ref_img.shape[0]))
|
| 68 |
+
# Apply color transfer
|
| 69 |
+
output_img = color_transfer(ref_img, input_img)
|
| 70 |
+
output_img_resize = cv2.resize(output_img, (orig_w, orig_h))
|
| 71 |
+
st.image(output_img_resize, caption="Result", width=500, channels="BGR")
|
| 72 |
+
|
| 73 |
+
_, buffer = cv2.imencode(".jpg", output_img_resize)
|
| 74 |
+
img_bytes = io.BytesIO(buffer)
|
| 75 |
+
|
| 76 |
+
# Add Download Button
|
| 77 |
+
st.download_button(
|
| 78 |
+
label="Download Image",
|
| 79 |
+
data=img_bytes,
|
| 80 |
+
file_name="image.jpg",
|
| 81 |
+
mime="image/jpeg"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
main()
|