Skip to content

Instantly share code, notes, and snippets.

@stephanie-w
Created April 6, 2021 18:15
Show Gist options
  • Select an option

  • Save stephanie-w/60c800df360f19e0ad1a1beafbe00b4a to your computer and use it in GitHub Desktop.

Select an option

Save stephanie-w/60c800df360f19e0ad1a1beafbe00b4a to your computer and use it in GitHub Desktop.
from PIL import Image
import numpy as np
cover = Image.open("cover.png")
data_c = np.array(cover)
# Convert image to full black and white and resize to cover image
secret = Image.open("qr-secret.png").convert('1')
secret = secret.resize(cover.size)
data_s = np.array(secret, dtype=np.uint8)
# Extract 2nd LSB (7th bit from left) from cover image
# The binary operation performed is : pixel >> 8-k & 1 (with k the kth bit to extract starting from 1)
# same as pixel // 2**(8-k) & 1
bit = np.bitwise_and(np.right_shift(data_c, 1), 1)
# Rewrite cover LSB with bit from secret xored with LSBs
res = data_c & ~1 | bit ^ data_s
new_img = Image.fromarray(res).convert("L")
new_img.save("cover-secret.png")
new_img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment