除了使用目录引用图像外,还可以将图像直接编码到程序中吗?

最佳答案

您可以使用base64模块将数据嵌入到程序中。从base64 documentation:

>>> import base64
>>> encoded = base64.b64encode('data to be encoded')
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
'data to be encoded'

使用此功能,您可以对图像进行base64编码,并将结果字符串嵌入程序中。要获取原始图像数据,您可以将该字符串传递给base64.b64decode

关于python - 是否可以将图像编码为python脚本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3715244/

10-16 11:06