Skip to content

Instantly share code, notes, and snippets.

@haile01
Created June 26, 2022 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haile01/08f8e171dd4c9cb3ba8cbd16a431d25a to your computer and use it in GitHub Desktop.
Save haile01/08f8e171dd4c9cb3ba8cbd16a431d25a to your computer and use it in GitHub Desktop.
Simple python script to embed message image into carrier image using lsb :>
import cv2
import sys
import numpy as np
def hide(carrier_file, message_file, output_file = 'result.png', bit_plane = 0, channel = 0):
carrier = cv2.imread(carrier_file)
message = cv2.imread(message_file)
output = carrier.copy()
cw, ch, _ = carrier.shape
mw, mh, _ = message.shape
# clear the current bit plane with white color
clear_mask = 2 ** bit_plane
for i in range(cw):
for j in range(ch):
output[i][j][channel] |= clear_mask
# embed the message
for i in range(min(cw, mw)):
for j in range(min(ch, mh)):
if np.all(message[i][j] == 0):
output[i][j][channel] ^= 2 ** bit_plane
cv2.imwrite(output_file, output)
if __name__ == "__main__":
carrier_file = sys.argv[1]
message_file = sys.argv[2]
bit_plane = int(sys.argv[3])
channel = int(sys.argv[4])
hide(carrier_file, message_file, bit_plane=bit_plane, channel=channel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment