【问题标题】:Best way to convert image,videos to string format将图像、视频转换为字符串格式的最佳方法
【发布时间】:2021-11-09 08:12:51
【问题描述】:

我想以加密格式存储我的一些个人图像和视频,并想自己开发python脚本。由于我使用的加密需要以字符串格式输入,将图像和视频转换为字符串格式的最有效方法是什么?

我曾考虑使用 base64,但由于它的大小增加,它不是一个实用且最有效的解决方案。

关于如何实现这一点的任何建议?

【问题讨论】:

    标签: python algorithm image encryption


    【解决方案1】:

    我尝试使用 python 的枕头模块将图像转换为字符串。

    from io import BytesIO
    from PIL import Image
    
    image = Image.open(r"*****\naruto.png")#The full path to the image
    
    output = BytesIO()
    image.save(output, format="png")
    image_as_string = output.getvalue()
    
    print(image_as_string[:20])
    

    My image

    输出:

    b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x05\xa0'

    这对我有用。我是编程初学者。所以其他建议也会有所帮助。

    【讨论】:

    • stackoverflow.com/a/17686777/15821025你可以参考这个。
    • 不,这是不对的。当您使用 Image.open() 时,您会将压缩的 PNG 图像解码到内存中,它本质上是一个未压缩的像素数组。当您执行Image.save() 时,您会将图像重新压缩回或多或少与最初在磁盘上的完全相同 - 所以这样做没有任何意义。您不妨使用标准的open()read()binary 模式打开图像。你会得到同样的结果。如果您不相信我,请转到 hexed.it 并加载您的原始 PNG,您会看到它与您创建的 "string" 相同。
    • @MarkSetchell 谢谢你的解释
    猜你喜欢
    • 2014-06-21
    • 2010-10-10
    • 2010-09-20
    • 2011-12-27
    • 2017-07-26
    • 1970-01-01
    • 2014-01-05
    • 2010-10-22
    相关资源
    最近更新 更多