[英]how to store an image in a variable and pass that variable as a argument in another function?
I need to store an image in a variable and pass that variable as a argument in another function?我需要将图像存储在变量中并将该变量作为参数传递给另一个 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.在另一个 function 中,我需要解密图像并按原样获取
a
和b
。 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.
然后图像从 A 传输到 B。B 需要在 A 编码之前按原样解密消息。
You can accomplish this using the BytesIO class from the io module like so:您可以使用io模块中的 BytesIO class 来完成此操作,如下所示:
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: 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.不是返回秘密,而是返回包含图像的字节 object,允许您将该图像存储在变量中并将其传递给另一个 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:我忘了提到这会将您的隐写图像保存在 memory 中,如果您想将图像保存到磁盘,您可以添加如下内容:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.