-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_solver.py
More file actions
160 lines (125 loc) · 4.79 KB
/
image_solver.py
File metadata and controls
160 lines (125 loc) · 4.79 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
import os
import numpy as np
from PIL import Image
# ++++++++++ MAIN ++++++++++
def run(images):
for img in images:
process_single_image(img)
process_multiple_images(images)
# Change File Name here
IMAGES = ["file2.jpg", "file3.jpg"]
# +++++++++ OUTPUT ++++++++
def get_output_dir(base="OUTPUT"):
i = 1
while os.path.exists(f"{base}_{i}"):
i += 1
path = f"{base}_{i}"
os.makedirs(path)
return path
OUTPUT_DIR = get_output_dir("OUTPUT")
# Folders for single and double image ops
SINGLE_DIR = os.path.join(OUTPUT_DIR, "SINGLE")
DOUBLE_DIR = os.path.join(OUTPUT_DIR, "DOUBLE")
os.makedirs(SINGLE_DIR, exist_ok=True)
os.makedirs(DOUBLE_DIR, exist_ok=True)
# ++++++++++ CONFIG ++++++++++
EXTRACT_BITS = range(8)
THRESH_MIN = 10
THRESH_MAX = 240
THRESH_STEP = 10
# ++++++++++ UTIL ++++++++++
#SL
def load_rgba(path):
if not os.path.exists(path):
raise FileNotFoundError(f"File not found: {path}")
return np.array(Image.open(path).convert("RGBA"), dtype=np.uint8)
def save_gray(arr, folder, name):
Image.fromarray(arr.astype(np.uint8), mode="L").save(os.path.join(folder, name))
def save_rgba(arr, folder, name):
Image.fromarray(arr.astype(np.uint8), mode="RGBA").save(os.path.join(folder, name))
#grayscale
def grayscale_standard(arr):
return (0.299*arr[:,:,0] + 0.587*arr[:,:,1] + 0.114*arr[:,:,2]).astype(np.uint8)
def grayscale_average(arr):
return np.mean(arr[:,:,:3], axis=2).astype(np.uint8)
# ++++++++++ LSB ++++++++++
def extract_lsb_text(arr):
bits = "".join(str(v & 1) for v in arr.flatten())
out = ""
for i in range(0, len(bits), 8):
byte = bits[i:i+8]
if len(byte) < 8:
continue
val = int(byte, 2)
out += chr(val) if 32 <= val <= 126 else "?"
return out
# ++++++++++ SINGLE IMAGE ++++++++++
def process_single_image(path):
name = os.path.splitext(os.path.basename(path))[0]
arr = load_rgba(path)
single_subdir = os.path.join(SINGLE_DIR, name)
os.makedirs(single_subdir, exist_ok=True)
# Rotate
save_rgba(np.rot90(arr, 1), single_subdir, f"{name}_rot_90.png")
save_rgba(np.rot90(arr, 2), single_subdir, f"{name}_rot_180.png")
save_rgba(np.rot90(arr, 3), single_subdir, f"{name}_rot_270.png")
# Flip
save_rgba(np.flip(arr, axis=1), single_subdir, f"{name}_flip_h.png")
save_rgba(np.flip(arr, axis=0), single_subdir, f"{name}_flip_v.png")
# Grayscale
gray_std = grayscale_standard(arr)
gray_avg = grayscale_average(arr)
save_gray(gray_std, single_subdir, f"{name}_gray_std.png")
save_gray(gray_avg, single_subdir, f"{name}_gray_avg.png")
# LSB
lsb_file = os.path.join(single_subdir, f"111{name}_lsb.txt") #put txt at front of file
with open(lsb_file, "w") as f:
channels = {
"R": arr[:,:,0],
"G": arr[:,:,1],
"B": arr[:,:,2],
"A": arr[:,:,3],
"GRAY_STD": gray_std,
"GRAY_AVG": gray_avg
}
for cname, chan in channels.items():
for bit in EXTRACT_BITS:
plane = ((chan >> bit) & 1) * 255
save_gray(plane, single_subdir, f"{name}_{cname}_bit{bit}.png")
text = extract_lsb_text(chan >> bit)
f.write(f"{cname} bit {bit}:\n{text}\n\n")
# THreshold
for t in range(THRESH_MIN, THRESH_MAX+1, THRESH_STEP):
bw = np.where(gray_std > t, 255, 0)
save_gray(bw, single_subdir, f"{name}_threshold_{t}.png")
# ++++++++++ DOUBLE IMAGE ++++++++++
def process_double_image(path_a, path_b):
name_a = os.path.splitext(os.path.basename(path_a))[0]
name_b = os.path.splitext(os.path.basename(path_b))[0]
double_subdir = os.path.join(DOUBLE_DIR, f"{name_a}_{name_b}")
os.makedirs(double_subdir, exist_ok=True)
a = load_rgba(path_a)
b = load_rgba(path_b)
ops = {
"xor": lambda x,y: x ^ y,
"and": lambda x,y: x & y,
"or": lambda x,y: x | y,
"add": lambda x,y: np.clip(x + y, 0, 255),
"sub": lambda x,y: np.clip(abs(x - y), 0, 255)
}
channels_a = [a[:,:,0], a[:,:,1], a[:,:,2], a[:,:,3], grayscale_standard(a), grayscale_average(a)]
channels_b = [b[:,:,0], b[:,:,1], b[:,:,2], b[:,:,3], grayscale_standard(b), grayscale_average(b)]
channel_names = ["R","G","B","A","GRAY_STD","GRAY_AVG"]
for op_name, fn in ops.items():
for idx, cname in enumerate(channel_names):
result = fn(channels_a[idx], channels_b[idx]).astype(np.uint8)
save_gray(result, double_subdir, f"{name_a}_{name_b}_{op_name}_{cname}.png")
def process_multiple_images(paths):
if len(paths) < 2:
return
for i in range(len(paths)-1):
for j in range(i+1, len(paths)):
process_double_image(paths[i], paths[j])
# +++++++++ START ++++++++++
run(IMAGES)