0

I need to store an image in a variable and pass that variable as a argument in another function?

from stegano import lsb

#Function to hide message in an image 
def hide():
    a = 12345678
    b = 'sdghgnh'
    c = (a, b)
    secret = lsb.hide("images/2.png",str(c))
    secret = secret.save("new.png")
    return secret

#Another function to decrypt the message as it is
def unhide(secret):

In another function I need to decrypt the image and get a and b as it is. The above functions are for public key steganography where messages are ciphered first and hidden in an image. Then the image is transferred from A to B. B needs to decrypt the message as it is before encoding by A.

Rashik
  • 21
  • 7
  • Does this answer your question? [Python: process image and save to file stream](https://stackoverflow.com/questions/14304245/python-process-image-and-save-to-file-stream) – Reti43 Mar 29 '21 at 13:33

1 Answers1

0

You can accomplish this using the BytesIO class from the io module like so:

from stegano import lsb
from io import BytesIO

# Function to hide message in an image
def hide():
    a = 12345678
    b = 'sdghgnh'
    c = (a, b)
    secret = lsb.hide("./0.png", str(c))
    arr = BytesIO()
    secret = secret.save(arr, format='PNG')
    arr.seek(0)
    return arr


# Another function to decrypt the message as it is
def unhide(secret):
    return lsb.reveal(secret)


img = hide()
print(unhide(img))

Output:

(12345678, 'sdghgnh')

Instead of returning secret you return a bytes object containing your image allowing you to store that image in a variable and pass it to another function.

EDIT
I forgot to mention that this keeps your steganographic image in memory, if you want to save the image to disk you could add something like this:

def hide():
    a = 12345678
    b = 'sdghgnh'
    c = (a, b)
    secret = lsb.hide("./0.png", str(c))
    arr = BytesIO()
    secret = secret.save(arr, format='PNG')
    arr.seek(0)
    with open('new.png', 'wb') as f:
        f.write(arr.read())
    return arr
TrevoR
  • 3
  • 2