mirs44 commited on
Commit
176f078
·
verified ·
1 Parent(s): e190f6e

Delete raveblender.py

Browse files
Files changed (1) hide show
  1. raveblender.py +0 -160
raveblender.py DELETED
@@ -1,160 +0,0 @@
1
- import gradio as gr
2
- from PIL import Image, ImageOps
3
- import numpy as np
4
- import os
5
- import uuid
6
- import random
7
- from scipy import ndimage
8
-
9
- # Ensure there's a directory for outputs
10
-
11
- os.makedirs("outputs", exist\_ok=True)
12
-
13
- def make\_square(img, size=3000, fill\_color=(0, 0, 0)):
14
- x, y = img.size
15
- scale = size / max(x, y)
16
- new\_size = (int(x \* scale), int(y \* scale))
17
- img = img.resize(new\_size, Image.Resampling.LANCZOS)
18
- new\_img = Image.new("RGB", (size, size), fill\_color)
19
- new\_img.paste(img, ((size - new\_size\[0]) // 2, (size - new\_size\[1]) // 2))
20
- return new\_img
21
-
22
- def pixel\_shuffle(img\_array, block\_size=10, shuffle\_strength=0.5):
23
- """Shuffle pixels in blocks to create generative effect"""
24
- height, width, channels = img\_array.shape
25
- result = np.copy(img\_array)
26
-
27
- ```
28
- # Create blocks for shuffling
29
- h_blocks = height // block_size
30
- w_blocks = width // block_size
31
-
32
- # Create list of block coordinates
33
- blocks = []
34
- for i in range(h_blocks):
35
- for j in range(w_blocks):
36
- blocks.append((i, j))
37
-
38
- # Shuffle a percentage of blocks based on strength
39
- num_blocks_to_shuffle = int(len(blocks) * shuffle_strength)
40
- blocks_to_shuffle = random.sample(blocks, num_blocks_to_shuffle)
41
-
42
- # Create a shuffled version of these blocks
43
- target_positions = blocks_to_shuffle.copy()
44
- random.shuffle(target_positions)
45
-
46
- # Perform the shuffling
47
- for (src_i, src_j), (tgt_i, tgt_j) in zip(blocks_to_shuffle, target_positions):
48
- src_y, src_x = src_i * block_size, src_j * block_size
49
- tgt_y, tgt_x = tgt_i * block_size, tgt_j * block_size
50
-
51
- # Swap blocks
52
- temp = np.copy(result[src_y:src_y+block_size, src_x:src_x+block_size])
53
- result[src_y:src_y+block_size, src_x:src_x+block_size] = result[tgt_y:tgt_y+block_size, tgt_x:tgt_x+block_size]
54
- result[tgt_y:tgt_y+block_size, tgt_x:tgt_x+block_size] = temp
55
-
56
- return result
57
- ```
58
-
59
- def flow\_distortion(img\_array, strength=10):
60
- """Apply flow-based distortion to simulate generative models"""
61
- height, width, channels = img\_array.shape
62
- result = np.zeros\_like(img\_array, dtype=np.float32)
63
-
64
- ```
65
- # Create random flow fields for x and y directions
66
- flow_x = np.random.normal(0, strength, (height, width))
67
- flow_y = np.random.normal(0, strength, (height, width))
68
-
69
- # Smooth the flow fields
70
- flow_x = ndimage.gaussian_filter(flow_x, sigma=30)
71
- flow_y = ndimage.gaussian_filter(flow_y, sigma=30)
72
-
73
- # Create meshgrid for coordinate mapping
74
- y_coords, x_coords = np.meshgrid(np.arange(height), np.arange(width), indexing='ij')
75
-
76
- # Add flow to coordinates
77
- x_mapped = x_coords + flow_x
78
- y_mapped = y_coords + flow_y
79
-
80
- # Clip to ensure we stay within bounds
81
- x_mapped = np.clip(x_mapped, 0, width-1)
82
- y_mapped = np.clip(y_mapped, 0, height-1)
83
-
84
- # Sample from the original image using the warped coordinates
85
- for c in range(channels):
86
- result[:, :, c] = ndimage.map_coordinates(img_array[:, :, c], [y_mapped, x_mapped], order=1)
87
-
88
- return result
89
- ```
90
-
91
- def blend\_images\_with\_rearrangement(images, block\_size=20, shuffle\_strength=0.3, flow\_strength=5):
92
- if len(images) < 2:
93
- return "Upload at least two images.", None
94
-
95
- ```
96
- try:
97
- # Process images
98
- processed = []
99
- for img in images:
100
- try:
101
- processed.append(make_square(Image.open(img)))
102
- except Exception as e:
103
- return f"Error processing image: {str(e)}", None
104
-
105
- # Convert images to numpy arrays
106
- img_arrays = [np.array(img).astype(np.float32) for img in processed]
107
-
108
- # Create a base canvas
109
- base = np.zeros_like(img_arrays[0])
110
-
111
- # Divide the images into a grid and randomly select pixels from different images
112
- height, width, _ = base.shape
113
- for i in range(0, height, block_size):
114
- for j in range(0, width, block_size):
115
- # Get end coordinates for the block
116
- end_i = min(i + block_size, height)
117
- end_j = min(j + block_size, width)
118
-
119
- # Randomly select which image to pull this block from
120
- source_img = random.choice(img_arrays)
121
- base[i:end_i, j:end_j] = source_img[i:end_i, j:end_j]
122
-
123
- # Apply pixel shuffling to the composite image
124
- base = pixel_shuffle(base, block_size, shuffle_strength)
125
-
126
- # Apply flow distortion to further randomize
127
- base = flow_distortion(base, flow_strength)
128
-
129
- # Blend with original images to preserve some coherence
130
- for img_array in img_arrays:
131
- base = base * 0.7 + img_array * 0.3 / len(img_arrays)
132
-
133
- final = Image.fromarray(np.uint8(np.clip(base, 0, 255)))
134
-
135
- # Save to file
136
- output_path = f"outputs/amalgam_{uuid.uuid4().hex[:8]}.png"
137
- final.save(output_path)
138
-
139
- return final, output_path
140
- except Exception as e:
141
- return f"Error during blending: {str(e)}", None
142
- ```
143
-
144
- demo = gr.Interface(
145
- fn=blend\_images\_with\_rearrangement,
146
- inputs=\[
147
- gr.File(file\_types=\["image"], file\_count="multiple", label="Upload 2–5 stills"),
148
- gr.Slider(minimum=5, maximum=100, value=20, step=5, label="Block Size (pixels)"),
149
- gr.Slider(minimum=0.0, maximum=1.0, value=0.3, step=0.05, label="Shuffle Strength"),
150
- gr.Slider(minimum=0, maximum=20, value=5, step=1, label="Flow Distortion")
151
- ],
152
- outputs=\[
153
- gr.Image(label="Generated Image"),
154
- gr.File(label="Download Image")
155
- ],
156
- title="Amalgamator",
157
- description="Upload up to 5 stills. Outputs a 3000x3000 image with pixel rearrangement to create a truly generative look."
158
- )
159
-
160
- demo.launch()