Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save stephanie-w/d07b1f70868d60dc980b6232b7aa03f1 to your computer and use it in GitHub Desktop.

Revisions

  1. stephanie-w created this gist Apr 6, 2021.
    28 changes: 28 additions & 0 deletions stegano_lsb_encode.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import numpy as np
    from PIL import Image

    message = "Hello World!"

    # Encode the message in a serie of 8-bit values
    b_message = ''.join(["{:08b}".format(ord(x)) for x in message ])
    b_message = [int(x) for x in b_message]

    b_message_lenght = len(b_message)

    # Get the image pixel arrays
    with Image.open("cover.png") as img:
    width, height = img.size
    data = np.array(img)

    # Flatten the pixel arrays
    data = np.reshape(data, width*height*3)

    # Overwrite pixel LSB
    data[:b_message_lenght] = data[:b_message_lenght] & ~1 | b_message

    # Reshape back to an image pixel array
    data = np.reshape(data, (height, width, 3))

    new_img = Image.fromarray(data)
    new_img.save("cover-secret.png")
    new_img.show()