Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app_colab.py
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from IPython.display import clear_output
|
| 6 |
+
import imageio
|
| 7 |
+
|
| 8 |
+
def load_image(image_path, scale_percent=50):
|
| 9 |
+
"""Load and resize image"""
|
| 10 |
+
img = cv2.imread(image_path)
|
| 11 |
+
if img is None:
|
| 12 |
+
raise FileNotFoundError(f"Image not found: {image_path}")
|
| 13 |
+
|
| 14 |
+
width = int(img.shape[1] * scale_percent / 100)
|
| 15 |
+
height = int(img.shape[0] * scale_percent / 100)
|
| 16 |
+
img = cv2.resize(img, (width, height))
|
| 17 |
+
return img
|
| 18 |
+
|
| 19 |
+
def pencil_sketch(img, blur_ksize=21):
|
| 20 |
+
"""Convert image to pencil sketch"""
|
| 21 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 22 |
+
inv_gray = 255 - gray
|
| 23 |
+
blur = cv2.GaussianBlur(inv_gray, (blur_ksize, blur_ksize), 0)
|
| 24 |
+
sketch = cv2.divide(gray, 255 - blur, scale=256)
|
| 25 |
+
return sketch
|
| 26 |
+
|
| 27 |
+
def add_texture(sketch, intensity=15):
|
| 28 |
+
"""Add pencil texture to sketch"""
|
| 29 |
+
noise = np.random.normal(0, intensity, sketch.shape).astype(np.uint8)
|
| 30 |
+
sketch_textured = cv2.add(sketch, noise)
|
| 31 |
+
return sketch_textured
|
| 32 |
+
|
| 33 |
+
def animate_sketch(sketch, save_gif=False, gif_name="pencil_sketch_animation.gif", show_animation=True):
|
| 34 |
+
"""Animate sketch line by line"""
|
| 35 |
+
canvas = np.ones_like(sketch) * 255
|
| 36 |
+
y_indices, x_indices = np.where(sketch < 255)
|
| 37 |
+
coords = list(zip(x_indices, y_indices))
|
| 38 |
+
coords.sort(key=lambda pt: sketch[pt[1], pt[0]]) # draw dark pixels first
|
| 39 |
+
|
| 40 |
+
frames = []
|
| 41 |
+
for i, (x, y) in enumerate(coords):
|
| 42 |
+
canvas[y, x] = sketch[y, x]
|
| 43 |
+
if i % 200 == 0:
|
| 44 |
+
if show_animation:
|
| 45 |
+
clear_output(wait=True)
|
| 46 |
+
plt.imshow(canvas, cmap='gray')
|
| 47 |
+
plt.axis('off')
|
| 48 |
+
plt.show()
|
| 49 |
+
if save_gif:
|
| 50 |
+
frames.append(canvas.copy())
|
| 51 |
+
|
| 52 |
+
# Final frame
|
| 53 |
+
if show_animation:
|
| 54 |
+
clear_output(wait=True)
|
| 55 |
+
plt.imshow(canvas, cmap='gray')
|
| 56 |
+
plt.axis('off')
|
| 57 |
+
plt.show()
|
| 58 |
+
|
| 59 |
+
if save_gif:
|
| 60 |
+
imageio.mimsave(gif_name, frames, duration=0.05)
|
| 61 |
+
print(f"[INFO] GIF saved as {gif_name}")
|
| 62 |
+
|
| 63 |
+
return canvas
|
| 64 |
+
|
| 65 |
+
# -----------------------------
|
| 66 |
+
# Colab Usage Example
|
| 67 |
+
# -----------------------------
|
| 68 |
+
|
| 69 |
+
# 1️⃣ Upload your image in Colab:
|
| 70 |
+
from google.colab import files
|
| 71 |
+
uploaded = files.upload()
|
| 72 |
+
image_path = list(uploaded.keys())[0] # take the first uploaded file
|
| 73 |
+
|
| 74 |
+
# 2️⃣ Load the image
|
| 75 |
+
img = load_image(image_path, scale_percent=50)
|
| 76 |
+
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 77 |
+
plt.axis('off')
|
| 78 |
+
plt.title("Original Image")
|
| 79 |
+
plt.show()
|
| 80 |
+
|
| 81 |
+
# 3️⃣ Convert to pencil sketch
|
| 82 |
+
sketch = pencil_sketch(img, blur_ksize=21)
|
| 83 |
+
|
| 84 |
+
# 4️⃣ Add pencil texture
|
| 85 |
+
sketch_textured = add_texture(sketch, intensity=15)
|
| 86 |
+
|
| 87 |
+
# 5️⃣ Animate and save as GIF
|
| 88 |
+
final_canvas = animate_sketch(sketch_textured, save_gif=True)
|
| 89 |
+
cv2.imwrite("final_sketch.png", final_canvas)
|
| 90 |
+
print("[INFO] Final sketch saved as final_sketch.png")
|