-
Notifications
You must be signed in to change notification settings - Fork 1
/
lsb_detect.py
executable file
·41 lines (32 loc) · 1.04 KB
/
lsb_detect.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
#!/usr/bin/env python3
'''
Detect LSB by decomposing an RGB image in layers. The result is 8 images,
each one reflecting the value of a bit of the colors.
'''
# pip install Pillow
from PIL import Image
from os.path import splitext
import sys
if len(sys.argv) != 2:
print(f'Syntax: {sys.argv[0]} <image>')
exit(-1)
file_input = sys.argv[1]
filename, extension = splitext(file_input)
def extract_lsb(size, pixels, bit):
width, height = size
output = Image.new('RGB', size)
new_pixels = output.load()
for x in range(width):
for y in range(height):
colors = pixels[x, y]
nr, ng, nb = [ ( (x >> bit) & 1 ) * 255 for x in (colors[0], colors[1], colors[2]) ]
new_pixels[x, y] = (nr, ng, nb)
return output
input = Image.open(file_input)
size = input.size
pixels = input.load()
for bit in range(8):
file_output = f'{filename}_bit{bit}{extension}'
print(f'Extracting bit {bit} in {file_output}')
output = extract_lsb(size, pixels, bit)
output.save(file_output)