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.

Revisions

  1. stephanie-w created this gist Apr 6, 2021.
    22 changes: 22 additions & 0 deletions stegano_lsb_qr_encode2.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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()