Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法重新编码base64解码的png图像文件数据_Python_Png - Fatal编程技术网

Python 无法重新编码base64解码的png图像文件数据

Python 无法重新编码base64解码的png图像文件数据,python,png,Python,Png,我用python读取png文件时遇到问题,我有一个web服务器来保存数据和读取数据,但我的“保存数据”一方在base64中接收图像以将其转换为png文件,并使用此文件 我创建了指向图像的路径,例如images/2017-07-23-18-46-30st1folio1.png imgBase64包含从我的应用程序(其他主题)发送的图像。 path=“images/”+str(日期时间路径)+str(数据['ST_字符串])+str(数据['Folio_字符串])+”.png“ 然后我用这个保存我的

我用python读取
png
文件时遇到问题,我有一个web服务器来保存数据和读取数据,但我的“保存数据”一方在base64中接收图像以将其转换为png文件,并使用此文件

我创建了指向图像的路径,例如
images/2017-07-23-18-46-30st1folio1.png

imgBase64
包含从我的应用程序(其他主题)发送的图像。
path=“images/”+str(日期时间路径)+str(数据['ST_字符串])+str(数据['Folio_字符串])+”.png

然后我用这个保存我的数据

with open(path, "wb") as fh:
    fh.write(imgBase64.decode('base64'))
for ix in photos:
     print "Access to path, "+ "./"+str(ix.idSTFolio.path_img)
     encoded_string = None
     with open("./"+str(ix.idSTFolio.path_img), "rb") as image_file:
           encoded_string = b64encode(image_file.read())
     print "encoded -> ", encoded_string
在这一刻,我可以看到我的照片没有问题

但我的问题是当我尝试用这个来读取数据时

with open(path, "wb") as fh:
    fh.write(imgBase64.decode('base64'))
for ix in photos:
     print "Access to path, "+ "./"+str(ix.idSTFolio.path_img)
     encoded_string = None
     with open("./"+str(ix.idSTFolio.path_img), "rb") as image_file:
           encoded_string = b64encode(image_file.read())
     print "encoded -> ", encoded_string
在每次迭代中,我都可以看到这条路线,当我打印第一行时,我会看到:

./images/2017-07-23-18-46-30st1folio1.png
但是最后一行只打印空字符串,我不知道为什么。如果我的文件存在,并且我尝试写入
foo.zip
(该文件不存在),返回的错误是它不存在,但是当我尝试打印
image\u文件时,我会得到以下结果:

<open file u'./images/2017-07-23-18-46-30st1folio1.png', mode 'rb' at 0x108123db0>

当我运行我的web服务器时,我的路径是
/Users/MyComputer/Documents/Codes/trackerServer
,我的文件夹
/images
,其中包含以前发送的图像。有什么想法吗

屏幕截图


什么是imgBase64.decode和b64 encode?我的猜测是其中一个,或者两者都不能正常工作或者不兼容。IMGBASE64是一个变量,可以从我的应用程序中接收base64中的图像,例如
IVBORW0KGGOAANSUHE…9CQAABJRU5ERKJGGG==
与此类型的
和b64encode是来自
的定义,从base64导入b64encode,B64解码
是否可以在输出目录中正确查看图像?x.idSTFolio.path\u img是否指向同一路径?否则,在通过b64encode运行image_file.read()之前,请先了解其结果/长度。我怀疑你不知怎么读到了空文件。您是否在同时读取和写入目录?查看
imgBase64.decode('base64')
中的数据是否与读回时在
encoded\u string
中得到的数据相同。如果不是,那就是问题的根源。换句话说,当调试一些你认为应该有效的东西时,开始验证你的假设,但是没有…是的,我更新了我的问题,我实现了这个测试并返回
True
打印“path->”,os.path.isfile(“./”+str(ix.idSTFolio.path\u img))
,插入\u数据和获取\u数据是来自我的web服务器的不同路径。