我正在尝试获取imag的base64字符串

2024-04-26 23:11:20 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试获取用户在本地上载的图像的base64字符串。 我的html代码是-

<div class="col-md-4">
      <label class="control-label"></label>
      <input type="file" name="galleryImage" class="filestyle" data-buttonText="Select a Image">
      <input class="btn btn-sm btn-info btn-block" name="getImageUrl" type="submit" Style="width: 100px;">
</div>

在我看来

galleryImageURL = request.POST.get('galleryImage')
print(galleryImageURL)
galleryImage = requests.get(galleryImageURL).content
print(galleryImage)
base64EncodedImageString = imageBase64Encode(galleryImage)
print(base64EncodedImageString)

def imageBase64Encode(galleryImage):
    with open(galleryImage, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
    return encoded_string

我犯了个错误

Invalid URL '763814bc03a9838a7bae23ad4867ec01.jpg': No schema supplied. Perhaps you meant http://763814bc03a9838a7bae23ad4867ec01.jpg?

Tags: namedivinputgettypelabelclassfile
2条回答

没有模式意味着您需要向路径提供http://(或https://)。你知道吗

编辑您的路径以http://开始,它应该可以工作。你知道吗

这里galleryImage是一个文件实例,而不是url。 所以你看错了。你知道吗

在你看来试试这个。你知道吗

galleryImage = request.FILES['galleryImage'].read()
base64string = base64.encodestring(galleryImage)

也别忘了在表单中添加enctype="multipart/form-data"。你知道吗

相关问题 更多 >