tahiraskarli commited on
Commit
32e281d
·
verified ·
1 Parent(s): 69b10a2

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. Aysun.py +111 -0
  2. README.md +1 -7
Aysun.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageFilter, ImageDraw
3
+ import numpy as np
4
+ import math
5
+ import os
6
+
7
+ # ========= CONFIG ==========
8
+ STATIC_TARGET_PATH = "/mnt/c/Users/Tahir/Desktop/llm_engineering/176132714935692938.jpg" # 👈 change this to your fixed target image path
9
+ OUTPUT_DIR = "outputs"
10
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
11
+ # ===========================
12
+
13
+ # Smooth easing function for animation
14
+ def ease_in_out_cubic(x):
15
+ return 4 * x**3 if x < 0.5 else 1 - pow(-2 * x + 2, 3) / 2
16
+
17
+ # Curved transition path
18
+ def curved_path(src_x, src_y, tgt_x, tgt_y, t):
19
+ x = src_x * (1 - t) + tgt_x * t
20
+ y = src_y * (1 - t) + tgt_y * t
21
+
22
+ dx = tgt_x - src_x
23
+ dy = tgt_y - src_y
24
+ length = math.hypot(dx, dy) + 1e-6
25
+ offset = math.sin(t * math.pi) * length * 0.1 # curvature factor
26
+ perp_x = -dy / length
27
+ perp_y = dx / length
28
+
29
+ x += perp_x * offset
30
+ y += perp_y * offset
31
+ return int(x), int(y)
32
+
33
+ # Core animation function
34
+ def rearrange_pixels_with_animation(source_img, target_img, frames=60, frame_duration=100):
35
+ if isinstance(source_img, np.ndarray):
36
+ source_img = Image.fromarray(source_img)
37
+ if isinstance(target_img, np.ndarray):
38
+ target_img = Image.fromarray(target_img)
39
+
40
+ target_img = target_img.convert("RGB")
41
+ source_img = source_img.convert("RGB").resize(target_img.size)
42
+
43
+ src_arr = np.array(source_img)
44
+ tgt_arr = np.array(target_img)
45
+ height, width = src_arr.shape[:2]
46
+
47
+ src_flat = src_arr.reshape(-1, 3)
48
+ tgt_flat = tgt_arr.reshape(-1, 3)
49
+
50
+ src_idx = np.argsort(np.sum(src_flat, axis=1))
51
+ tgt_idx = np.argsort(np.sum(tgt_flat, axis=1))
52
+
53
+ coords = np.array([[y, x] for y in range(height) for x in range(width)])
54
+
55
+ frame_list = []
56
+ for f in range(frames + 1):
57
+ alpha = ease_in_out_cubic(f / frames)
58
+ frame_img = Image.new("RGB", (width, height))
59
+ draw = ImageDraw.Draw(frame_img)
60
+
61
+ for i, old_idx in enumerate(src_idx):
62
+ src_y, src_x = coords[old_idx]
63
+ tgt_y, tgt_x = coords[tgt_idx[i]]
64
+ curr_x, curr_y = curved_path(src_x, src_y, tgt_x, tgt_y, alpha)
65
+
66
+ if 0 <= curr_x < width and 0 <= curr_y < height:
67
+ draw.point((curr_x, curr_y), fill=tuple(src_flat[old_idx]))
68
+
69
+ frame_img = frame_img.filter(ImageFilter.GaussianBlur(radius=0.2))
70
+ frame_list.append(frame_img)
71
+
72
+ gif_path = os.path.join(OUTPUT_DIR, "pixel_animation.gif")
73
+ frame_list[0].save(
74
+ gif_path,
75
+ format='GIF',
76
+ save_all=True,
77
+ append_images=frame_list[1:],
78
+ duration=frame_duration,
79
+ loop=0,
80
+ optimize=True
81
+ )
82
+
83
+ return frame_list[-1], gif_path
84
+
85
+ # Wrapper function — uses static target image path
86
+ def convert_images_with_animation(source_image):
87
+ if not os.path.exists(STATIC_TARGET_PATH):
88
+ raise FileNotFoundError(f"Static target image not found: {STATIC_TARGET_PATH}")
89
+
90
+ target_image = Image.open(STATIC_TARGET_PATH)
91
+ final_img, gif_path = rearrange_pixels_with_animation(source_image, target_image)
92
+ return final_img, gif_path
93
+
94
+ # ---------------- Gradio UI ----------------
95
+ with gr.Blocks() as demo:
96
+ gr.Markdown("## 🎨 Pixel Rearrangement Tool (Static Target Version)")
97
+
98
+ source_input = gr.Image(label="Upload Source Image", type="pil")
99
+ convert_button = gr.Button("Convert & Animate")
100
+
101
+ with gr.Row():
102
+ output_image = gr.Image(label="Converted Image")
103
+ output_gif = gr.File(label="Animation GIF")
104
+
105
+ convert_button.click(
106
+ fn=convert_images_with_animation,
107
+ inputs=[source_input],
108
+ outputs=[output_image, output_gif]
109
+ )
110
+
111
+ demo.launch(inbrowser=True, share=True)
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
  title: Aysun
3
- emoji: 🐢
4
- colorFrom: purple
5
- colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Aysun
3
+ app_file: Aysun.py
 
 
4
  sdk: gradio
5
  sdk_version: 5.49.1
 
 
6
  ---