由于“源丢失”而无法打开base64解码的“ .jpg”图像?

时间:2019-03-29 04:50:35

标签: python django image base64

我正在尝试使用.jpg发送一个request文件并尝试将其解码 django服务器端。

代码:

这是发送方代码:

import requests
import os
import base64
fPath = os.getcwd()
url = 'http://127.0.0.1:8000/submitcausedata/'
headers = {'content-type': 'application/x-www-form-urlencoded'}
path_img = fPath + '/image13.jpg'
data = open(path_img,'rb').read()
encoded_image = base64.encodestring(data)   
print(encoded_image[:10])
r = requests.post(url,data=encoded_image,headers=headers)   

在接收结束码时:

@csrf_exempt
def submitCauseData(request):   
    response_data = {}  
    if request.method == 'POST':
        data = request.POST     
        myDict = dict(data)     
        imageStr = list(myDict.keys())      
        imageStr = imageStr[0]
        print(imageStr[:10])
        image_result = open('image.jpg', 'wb')       
        image_result.write(base64.b64decode(imageStr))
        return HttpResponse("Page Exists")      

因此,代码正在执行,但是当我尝试打开保存的图像时,它显示错误Photo Source File missing

发送代码输出:

print(encoded_image[:10])
----> b'/9j/4WQYRX'

接收方代码输出:

print(imageStr[:10])
----> /9j/4WQYRX

更新:

使用.jpg转换比较两个.txt文件时,使用在线DiffChecker会比较两个文件中的许多值。

Ubuntu上的Image Viewer在打开接收端的.jpg时显示错误:

Error interpreting JPEG image file (Unsupported marker type 0x10)

也:

在发送方:

print(len(data))
print(type(data))
print(len(encoded_image))
print(type(encoded_image))

OUTPUT:

171062
<class 'bytes'>
228084
<class 'bytes'>

在接收方:

print(len(imageStr))
print(type(imageStr))
print(len(imagedec))
print(type(imagedec))

OUTPUT:

228083
<class 'str'>
168972
<class 'bytes'>

2 个答案:

答案 0 :(得分:0)

以下代码有效吗?

发送

from base64 import b64encode
from pathlib import Path

import requests


url = "http://127.0.0.1:8000/submitcausedata/"
headers = {"content-type": "application/x-www-form-urlencoded"}
encoded_image = b64encode(Path("image13.jpg").read_bytes())
print(encoded_image[:10])
r = requests.post(url, data=encoded_image, headers=headers)

接收

from base64 import b64decode
from pathlib import Path    

@csrf_exempt
def submitCauseData(request):
    response_data = {}
    if request.method == "POST":
        imageStr = list(dict(request.POST).keys())[0]
        print(imageStr[:10])
        p = Path("image.jpg")
        p.write_bytes(b64decode(imageStr))
        return HttpResponse(f"Saved image to `{p.resolve()}`")

答案 1 :(得分:0)

我发现了错误,因为当我尝试从发送方发送class的{​​{1}}字符串时,发生的情况是将所有byte'+'都转换为'='在接收方。

因此,通过使用:

发送方:

' '

接收方:

import requests
import os
import base64
fPath = os.getcwd()
url = 'http://127.0.0.1:8000/submitcausedata/'
headers = {'content-type': 'application/x-www-form-urlencoded'}
path_img = fPath + '/image13.jpg'
data = open(path_img,'rb').read()
encoded_image = base64.b64encode(data)  
r = requests.post(url,data=encoded_image,headers=headers)       

我解决了这个错误。

无论如何,感谢您的帮助@csrf_exempt def submitCauseData(request): response_data = {} if request.method == 'POST': data = request.POST myDict = dict(data) imageStr = list(myDict.keys()) image = imageStr[0].replace(' ','+') image = image.encode() + b'===' imagedec = base64.b64decode(image) fPath = os.getcwd() image_result = open(fPath + '/image.jpg', 'wb') image_result.write(imagedec) image_result.close() return HttpResponse("HELLO") :)