-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathe.py
56 lines (46 loc) · 1.42 KB
/
e.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
from PIL import Image
import random
# Hardcoded Sudoku
Kn = [
[3, 1, 2],
[1, 2, 3],
[2, 3, 1]
]
# Function to split the image into blocks of Sudoku size
def split_into_blocks(arr, nrows, ncols):
h, w = 9, 9
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
# Convert the image to grayscale and into a numpy array
img_array = [
[1, 2, 1, 2, 2, 2, 3, 3, 3],
[1, 1, 1, 2, 2, 2, 3, 3, 3],
[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6],
[4, 4, 4, 5, 5, 5, 6, 6, 6],
[4, 4, 4, 5, 5, 5, 6, 6, 6],
[7, 7, 7, 8, 8, 8, 9, 9, 9],
[7, 7, 7, 8, 8, 8, 9, 9, 9],
[7, 7, 7, 8, 8, 8, 9, 9, 9]
]
img_array = np.array(img_array)
# Apply the shuffling function to the image
blocks = split_into_blocks(img_array, 3, 3)
import numpy as np
# Original array
arr = blocks
# Save the indices of the original array
indices = np.arange(len(arr))
# Shuffle the array using the indices
np.random.shuffle(indices)
# Display the shuffled array
shuffled_arr = arr[indices]
print("Shuffled array:", shuffled_arr)
# Now, bring back the original array from the shuffled array
original_arr = shuffled_arr[np.argsort(indices)]
print("Original array:", original_arr)
# Convert the shuffled image back to an Image object and save it
shuffled_img = Image.fromarray(arr.astype(np.uint8))
shuffled_img.save('shuffled_image.png')