【问题标题】:how to store an image in a variable and pass that variable as a argument in another function?如何将图像存储在变量中并将该变量作为参数传递给另一个函数?
【发布时间】:2021-06-25 05:49:23
【问题描述】:

我需要将图像存储在变量中并将该变量作为参数传递给另一个函数?

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):

在另一个函数中,我需要解密图像并按原样获取ab。上述功能用于公钥隐写术,其中消息首先被加密并隐藏在图像中。然后图像从 A 传输到 B。B 需要在 A 编码之前将消息原样解密。

【问题讨论】:

标签: python python-3.x encryption cryptography steganography


【解决方案1】:

您可以使用 io 模块中的 BytesIO 类来完成此操作,如下所示:

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))

输出:

(12345678, 'sdghgnh')

您返回一个包含您的图像的字节对象而不是返回秘密,从而允许您将该图像存储在一个变量中并将其传递给另一个函数。

编辑
我忘了提到这会将您的隐写图像保存在内存中,如果您想将图像保存到磁盘,您可以添加如下内容:

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

【讨论】:

    猜你喜欢
    • 2015-09-28
    • 2014-04-02
    • 2022-01-19
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多