mirs44 commited on
Commit
2c983e6
·
verified ·
1 Parent(s): 13c0981

Upload 2 files

Browse files
Files changed (2) hide show
  1. godraveapp.py +224 -0
  2. nginx.rtf +13 -0
godraveapp.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """godraveapp.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1MHm84PnI1EaofvmNUzaIVeLj7kBB3FL1
8
+ """
9
+
10
+ !pip install gradio pillow
11
+
12
+ import gradio as gr
13
+ from PIL import Image, ImageOps
14
+ import numpy as np
15
+ import os
16
+ import uuid
17
+
18
+ # Ensure there's a directory for outputs
19
+ os.makedirs("outputs", exist_ok=True)
20
+
21
+ def make_square(img, size=3000, fill_color=(0, 0, 0)):
22
+ x, y = img.size
23
+ scale = size / max(x, y)
24
+ new_size = (int(x * scale), int(y * scale))
25
+ # Replace deprecated ANTIALIAS with modern equivalent
26
+ img = img.resize(new_size, Image.Resampling.LANCZOS)
27
+ new_img = Image.new("RGB", (size, size), fill_color)
28
+ new_img.paste(img, ((size - new_size[0]) // 2, (size - new_size[1]) // 2))
29
+ return new_img
30
+
31
+ def blend_images(images):
32
+ if len(images) < 2:
33
+ return "Upload at least two images.", None
34
+
35
+ try:
36
+ # Add error handling for image processing
37
+ processed = []
38
+ for img in images:
39
+ try:
40
+ processed.append(make_square(Image.open(img)))
41
+ except Exception as e:
42
+ return f"Error processing image: {str(e)}", None
43
+
44
+ base = np.array(processed[0]).astype(np.float32)
45
+
46
+ for img in processed[1:]:
47
+ base = (base + np.array(img).astype(np.float32)) / 2
48
+
49
+ final = Image.fromarray(np.uint8(base))
50
+
51
+ # Save to file
52
+ output_path = f"outputs/amalgam_{uuid.uuid4().hex[:8]}.png"
53
+ final.save(output_path)
54
+
55
+ return final, output_path
56
+ except Exception as e:
57
+ return f"Error during blending: {str(e)}", None
58
+
59
+ demo = gr.Interface(
60
+ fn=blend_images,
61
+ inputs=gr.File(file_types=["image"], file_count="multiple", label="Upload 2–5 stills"),
62
+ outputs=[
63
+ gr.Image(label="Blended Image"),
64
+ gr.File(label="Download Image")
65
+ ],
66
+ title="Amalgamator",
67
+ description="Upload up to 5 stills. Outputs a 3000x3000 blended image preserving the aesthetic. Save it as PNG below."
68
+ )
69
+
70
+ demo.launch()
71
+
72
+ import gradio as gr
73
+ from PIL import Image, ImageOps
74
+ import numpy as np
75
+ import os
76
+ import uuid
77
+ import random
78
+ from scipy import ndimage
79
+
80
+ # Ensure there's a directory for outputs
81
+ os.makedirs("outputs", exist_ok=True)
82
+
83
+ def make_square(img, size=3000, fill_color=(0, 0, 0)):
84
+ x, y = img.size
85
+ scale = size / max(x, y)
86
+ new_size = (int(x * scale), int(y * scale))
87
+ img = img.resize(new_size, Image.Resampling.LANCZOS)
88
+ new_img = Image.new("RGB", (size, size), fill_color)
89
+ new_img.paste(img, ((size - new_size[0]) // 2, (size - new_size[1]) // 2))
90
+ return new_img
91
+
92
+ def pixel_shuffle(img_array, block_size=10, shuffle_strength=0.5):
93
+ """Shuffle pixels in blocks to create generative effect"""
94
+ height, width, channels = img_array.shape
95
+ result = np.copy(img_array)
96
+
97
+ # Create blocks for shuffling
98
+ h_blocks = height // block_size
99
+ w_blocks = width // block_size
100
+
101
+ # Create list of block coordinates
102
+ blocks = []
103
+ for i in range(h_blocks):
104
+ for j in range(w_blocks):
105
+ blocks.append((i, j))
106
+
107
+ # Shuffle a percentage of blocks based on strength
108
+ num_blocks_to_shuffle = int(len(blocks) * shuffle_strength)
109
+ blocks_to_shuffle = random.sample(blocks, num_blocks_to_shuffle)
110
+
111
+ # Create a shuffled version of these blocks
112
+ target_positions = blocks_to_shuffle.copy()
113
+ random.shuffle(target_positions)
114
+
115
+ # Perform the shuffling
116
+ for (src_i, src_j), (tgt_i, tgt_j) in zip(blocks_to_shuffle, target_positions):
117
+ src_y, src_x = src_i * block_size, src_j * block_size
118
+ tgt_y, tgt_x = tgt_i * block_size, tgt_j * block_size
119
+
120
+ # Swap blocks
121
+ temp = np.copy(result[src_y:src_y+block_size, src_x:src_x+block_size])
122
+ 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]
123
+ result[tgt_y:tgt_y+block_size, tgt_x:tgt_x+block_size] = temp
124
+
125
+ return result
126
+
127
+ def flow_distortion(img_array, strength=10):
128
+ """Apply flow-based distortion to simulate generative models"""
129
+ height, width, channels = img_array.shape
130
+ result = np.zeros_like(img_array, dtype=np.float32)
131
+
132
+ # Create random flow fields for x and y directions
133
+ flow_x = np.random.normal(0, strength, (height, width))
134
+ flow_y = np.random.normal(0, strength, (height, width))
135
+
136
+ # Smooth the flow fields
137
+ flow_x = ndimage.gaussian_filter(flow_x, sigma=30)
138
+ flow_y = ndimage.gaussian_filter(flow_y, sigma=30)
139
+
140
+ # Create meshgrid for coordinate mapping
141
+ y_coords, x_coords = np.meshgrid(np.arange(height), np.arange(width), indexing='ij')
142
+
143
+ # Add flow to coordinates
144
+ x_mapped = x_coords + flow_x
145
+ y_mapped = y_coords + flow_y
146
+
147
+ # Clip to ensure we stay within bounds
148
+ x_mapped = np.clip(x_mapped, 0, width-1)
149
+ y_mapped = np.clip(y_mapped, 0, height-1)
150
+
151
+ # Sample from the original image using the warped coordinates
152
+ for c in range(channels):
153
+ result[:, :, c] = ndimage.map_coordinates(img_array[:, :, c], [y_mapped, x_mapped], order=1)
154
+
155
+ return result
156
+
157
+ def blend_images_with_rearrangement(images, block_size=20, shuffle_strength=0.3, flow_strength=5):
158
+ if len(images) < 2:
159
+ return "Upload at least two images.", None
160
+
161
+ try:
162
+ # Process images
163
+ processed = []
164
+ for img in images:
165
+ try:
166
+ processed.append(make_square(Image.open(img)))
167
+ except Exception as e:
168
+ return f"Error processing image: {str(e)}", None
169
+
170
+ # Convert images to numpy arrays
171
+ img_arrays = [np.array(img).astype(np.float32) for img in processed]
172
+
173
+ # Create a base canvas
174
+ base = np.zeros_like(img_arrays[0])
175
+
176
+ # Divide the images into a grid and randomly select pixels from different images
177
+ height, width, _ = base.shape
178
+ for i in range(0, height, block_size):
179
+ for j in range(0, width, block_size):
180
+ # Get end coordinates for the block
181
+ end_i = min(i + block_size, height)
182
+ end_j = min(j + block_size, width)
183
+
184
+ # Randomly select which image to pull this block from
185
+ source_img = random.choice(img_arrays)
186
+ base[i:end_i, j:end_j] = source_img[i:end_i, j:end_j]
187
+
188
+ # Apply pixel shuffling to the composite image
189
+ base = pixel_shuffle(base, block_size, shuffle_strength)
190
+
191
+ # Apply flow distortion to further randomize
192
+ base = flow_distortion(base, flow_strength)
193
+
194
+ # Blend with original images to preserve some coherence
195
+ for img_array in img_arrays:
196
+ base = base * 0.7 + img_array * 0.3 / len(img_arrays)
197
+
198
+ final = Image.fromarray(np.uint8(np.clip(base, 0, 255)))
199
+
200
+ # Save to file
201
+ output_path = f"outputs/amalgam_{uuid.uuid4().hex[:8]}.png"
202
+ final.save(output_path)
203
+
204
+ return final, output_path
205
+ except Exception as e:
206
+ return f"Error during blending: {str(e)}", None
207
+
208
+ demo = gr.Interface(
209
+ fn=blend_images_with_rearrangement,
210
+ inputs=[
211
+ gr.File(file_types=["image"], file_count="multiple", label="Upload 2–5 stills"),
212
+ gr.Slider(minimum=5, maximum=100, value=20, step=5, label="Block Size (pixels)"),
213
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.3, step=0.05, label="Shuffle Strength"),
214
+ gr.Slider(minimum=0, maximum=20, value=5, step=1, label="Flow Distortion")
215
+ ],
216
+ outputs=[
217
+ gr.Image(label="Generated Image"),
218
+ gr.File(label="Download Image")
219
+ ],
220
+ title="Amalgamator",
221
+ description="Upload up to 5 stills. Outputs a 3000x3000 image with pixel rearrangement to create a truly generative look."
222
+ )
223
+
224
+ demo.launch()
nginx.rtf ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {\rtf1\ansi\ansicpg1252\cocoartf2822
2
+ \cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
3
+ {\colortbl;\red255\green255\blue255;}
4
+ {\*\expandedcolortbl;;}
5
+ \margl1440\margr1440\vieww11520\viewh8400\viewkind0
6
+ \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
7
+
8
+ \f0\fs24 \cf0 gradio\
9
+ pillow\
10
+ numpy\
11
+ opencv-python\
12
+ scipy\
13
+ }