Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +25 -66
src/streamlit_app.py
CHANGED
|
@@ -1,82 +1,41 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import io
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import cv2
|
| 5 |
import numpy as np
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
st.set_page_config(page_title="Image → Sketch", page_icon="🎨", layout="centered")
|
| 9 |
-
st.title("Image → Sketch 🎨")
|
| 10 |
-
st.caption("
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"""
|
| 15 |
-
1) Görseli griye çevirir, 2) Tersini alır (invert), 3) Invert edilmiş görseli Gaussian blur ile yumuşatır,
|
| 16 |
-
4) `color dodge` (bölme) işlemi ile kurşun kalem efekti verir.
|
| 17 |
-
"""
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
if file:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 30 |
-
|
| 31 |
-
# Invert + Gaussian blur
|
| 32 |
-
inverted = 255 - gray
|
| 33 |
-
blurred = cv2.GaussianBlur(inverted, (blur_kernel, blur_kernel), 0)
|
| 34 |
|
| 35 |
-
# Color dodge (division)
|
| 36 |
-
sketch = cv2.divide(gray, 255 - blurred, scale=255)
|
| 37 |
-
|
| 38 |
-
st.subheader("Original vs Sketch")
|
| 39 |
col1, col2 = st.columns(2)
|
| 40 |
with col1:
|
| 41 |
-
st.image(
|
| 42 |
with col2:
|
| 43 |
-
st.image(sketch, caption=f"Sketch (
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
sketch_pil = Image.fromarray(sketch)
|
| 47 |
buf = io.BytesIO()
|
| 48 |
-
|
| 49 |
-
st.download_button(
|
| 50 |
-
label="Eskizi PNG olarak indir",
|
| 51 |
-
data=buf.getvalue(),
|
| 52 |
-
file_name="sketch.png",
|
| 53 |
-
mime="image/png",
|
| 54 |
-
)
|
| 55 |
else:
|
| 56 |
st.info("Devam etmek için bir görsel yükle.")
|
| 57 |
|
| 58 |
-
# requirements.txt
|
| 59 |
-
# -----------------
|
| 60 |
-
# streamlit==1.39.0
|
| 61 |
-
# opencv-python-headless==4.10.0.84
|
| 62 |
-
# pillow==10.4.0
|
| 63 |
-
# numpy==1.26.4
|
| 64 |
-
|
| 65 |
-
# runtime.txt (opsiyonel ama tavsiye edilir)
|
| 66 |
-
# ------------------------------------------
|
| 67 |
-
# python-3.10
|
| 68 |
-
|
| 69 |
-
# README.md
|
| 70 |
-
# ---------
|
| 71 |
-
# Image → Sketch (Streamlit)
|
| 72 |
-
# Basit bir portre eskiz çevirici. Streamlit üzerinde çalışır.
|
| 73 |
-
#
|
| 74 |
-
# 🚀 Nasıl Çalıştırılır (Hugging Face Spaces)
|
| 75 |
-
# 1) Yeni bir Space oluştur: **Create New Space → SDK: Streamlit**
|
| 76 |
-
# 2) Bu depoya `app.py`, `requirements.txt` ve tercihen `runtime.txt` dosyalarını ekle.
|
| 77 |
-
# 3) Space açılınca otomatik olarak build eder ve çalışır.
|
| 78 |
-
#
|
| 79 |
-
# Notlar
|
| 80 |
-
# - `opencv-python-headless` kullanıyoruz (GUI bağımlılıkları olmadan sunucu ortamında çalışır).
|
| 81 |
-
# - Bulanıklık çekirdek boyutu ayarlanabilir (tek sayı olmalı). Varsayılan: 21.
|
| 82 |
-
# - İndirme butonuyla çıktıyı PNG olarak alabilirsiniz.
|
|
|
|
|
|
|
| 1 |
import io
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PIL import Image, ImageFilter, ImageOps
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="Image → Sketch (Lite)", page_icon="🎨", layout="centered")
|
| 7 |
+
st.title("Image → Sketch (Lite) 🎨")
|
| 8 |
+
st.caption("PIL + NumPy ile basit kurşun kalem efekti (Gaussian blur + color dodge).")
|
| 9 |
|
| 10 |
+
file = st.file_uploader("Bir görsel yükle (jpg, png, jpeg)", type=["jpg", "png", "jpeg"])
|
| 11 |
+
radius = st.slider("Bulanıklık yarıçapı (Gaussian)", 1, 25, 8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def color_dodge(gray_img: Image.Image, blur_img: Image.Image) -> Image.Image:
|
| 14 |
+
"""PIL (L) iki resmi color-dodge ile birleştirir (NumPy ile)."""
|
| 15 |
+
g = np.asarray(gray_img).astype(np.float32)
|
| 16 |
+
b = np.asarray(blur_img).astype(np.float32)
|
| 17 |
+
# 255 - b yaklaştıkça bölme büyür; sıfıra bölmeyi engellemek için epsilon
|
| 18 |
+
eps = 1e-3
|
| 19 |
+
out = np.clip(g * 255.0 / (255.0 - b + eps), 0, 255).astype(np.uint8)
|
| 20 |
+
return Image.fromarray(out, mode="L")
|
| 21 |
|
| 22 |
if file:
|
| 23 |
+
img = Image.open(file).convert("RGB")
|
| 24 |
+
gray = img.convert("L")
|
| 25 |
+
inverted = ImageOps.invert(gray)
|
| 26 |
+
blurred = inverted.filter(ImageFilter.GaussianBlur(radius=radius))
|
| 27 |
+
sketch = color_dodge(gray, blurred)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
col1, col2 = st.columns(2)
|
| 30 |
with col1:
|
| 31 |
+
st.image(img, caption="Orijinal", use_container_width=True)
|
| 32 |
with col2:
|
| 33 |
+
st.image(sketch, caption=f"Sketch (radius={radius})", use_container_width=True, clamp=True)
|
| 34 |
|
| 35 |
+
# İndir
|
|
|
|
| 36 |
buf = io.BytesIO()
|
| 37 |
+
sketch.save(buf, format="PNG")
|
| 38 |
+
st.download_button("PNG olarak indir", data=buf.getvalue(), file_name="sketch.png", mime="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
st.info("Devam etmek için bir görsel yükle.")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|