我想把一张图片转换成字节,放到一个文本文件中,然后打开那个文本文件,再把它转换成一张图片。你知道吗
png=open("C:\\Users\\myUser\\Desktop\\n.png","rb")
pngbytes=png.read()
newf=open("C:\\Users\\myUser\\Desktop\\newf.txt","w")
newf.write(str(pngbytes))
newf.close()
newf=open("C:\\Users\\myUser\\Desktop\\newf.txt","r")
newpng=open("C:\\Users\\myUser\\Desktop\\newpng.png","wb")
strNewf=newf.read()
newpng.write(strNewf.encode())
newpng.close()
png.close()
newf.close()
图像已创建,但无法显示。你知道吗
通过用
eval(strNewf)
替换strNewf.encode()
,可以使代码正常工作。你知道吗这是因为用
str(pngbytes)
创建的字符串提供了字节的字符串表示形式,eval
只是解释该表示形式以再次提供字节。你知道吗你为什么要这样做是完全不清楚的,但是-你想实现什么?因为似乎有更好的方法。。。你知道吗
这里有一个完整的工作示例。你知道吗
这将: 1) 将图像文件加载到内存中。 2) 将图像转换为原始文本。 3) 将图像作为文本保存到其他文件中。 4) 读取文本文件并将其转换为原始图像。你知道吗
你不清楚内置函数'str'的结果
相关问题 更多 >
编程相关推荐