Skip to content

Instantly share code, notes, and snippets.

@yvki
Created May 4, 2024 06:33
Show Gist options
  • Save yvki/7c737f80ae323d4ac17e01fffed5b5c9 to your computer and use it in GitHub Desktop.
Save yvki/7c737f80ae323d4ac17e01fffed5b5c9 to your computer and use it in GitHub Desktop.
LSB Matching method πŸ”„οΈ in Python for Steganography
import sys, cv2, random
import numpy as np
def extract():
J=cv2.imread('image1.png')
f=open('output_payload.txt', 'w+', errors="ignore")
idx=0
bitidx=0
bitval=0
for i in range(J.shape[0]):
if (I[i, 0, 0] == '-'):
break
for j in range(J.shape[1]):
for k in range(3):
if (I[i , j, k] == '-'):
break
if bitidx=8:
f.write(chr(bitval))
bitidx=0
bitval=0
bitval!=(I[i, j, k]%2)<<bitidx
bitidx+=1
f.close()
bits=[]
f=open('payload.txt', 'r')
blist=[ord(b) for b in f.read()]
for b in blist:
for i in range(8):
bits.append((b >> i) & 1)
I = np.asarray(cv2.imread('C:\\Users\filepath\image2.png'))
sign=[1,-1]
idx=0
for i in range(I.shape[0]):
for j in range(I.shape[1]):
for k in range(3):
if idx<len(bits):
if I[i][j][k]%2 != bits[idx]:
s=sign[random.randint(0,1)]
if I[i][j][k]==0: s=1
if I[i][j][k]==255: s=-1
I[i][j][k]+=s
idx+=1
cv2.imread('image3.png', I)
print("Extracting...")
extract()
print("Completed!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment