Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 将Base 64字符串转换为BytesIO_Python_Json_Flask_Base64_Gcp - Fatal编程技术网

Python 将Base 64字符串转换为BytesIO

Python 将Base 64字符串转换为BytesIO,python,json,flask,base64,gcp,Python,Json,Flask,Base64,Gcp,感谢所有能帮上忙的人,这是我的第一个问题,希望这不是非常明显 我有一个作为base64字符串传递的图像(使用slim image cropper)。我想把它转换成一个文件,然后作为一个blob发送到Google存储 之前我只是发送了如下文件 image = request.files.get('image') client = _get_storage_client() bucket = client.bucket(current_app.config['CLOUD_STORAGE_BUCKET

感谢所有能帮上忙的人,这是我的第一个问题,希望这不是非常明显

我有一个作为base64字符串传递的图像(使用slim image cropper)。我想把它转换成一个文件,然后作为一个blob发送到Google存储

之前我只是发送了如下文件

image = request.files.get('image')
client = _get_storage_client()
bucket = client.bucket(current_app.config['CLOUD_STORAGE_BUCKET'])
blob = bucket.blob(filename)

blob.upload_from_string(
    image.read(),
    content_type=content_type)
现在我正在处理下面的代码

cropped_image = json.loads(request.form.get('slim[]'))
data = cropped_image['output']['image']
数据变量是一个字符串:

data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBD...'
我无法确定是否需要编码到base64,从base64解码,将其转换为bytestring,然后编码/解码

我试着用bytesIO和StringIO按原样发送它

image = BytesIO(data)
blob.upload_from_string(
    image.read(),
    content_type=content_type)
我上传了一张黑色图片,真的试图在没有研究的情况下不提问,但这张图片让我很困惑

谢谢

re.sub(“数据:image/jpeg;base64,”,b64_str)。解码(“base64”)
在Python2中工作。在Py2中,
str
实际上是
bytes

更新

from base64 import b64decode

with open("test.jpeg", 'wb') as f:
    f.write(b64decode(re.sub("data:image/jpeg;base64,", '', b64_str)))

# or

image = BytesIO(b64decode(re.sub("data:image/jpeg;base64", '', b64_str)))

upload\u from_string
的参数在第二种情况下与第一种情况下相同。从
数据创建的变量
image
,在调用
upload\u from\u string
时未使用。是这样吗?在第一个示例中,我传递了一个从web表单上载的文件存储。我已尝试发送数据变量以从_字符串上载_,并获得与使用bytesIO或StringIO相同的结果。如果数据变量保存字符串,我将如何做到这一点?像这样
re.sub(data',b64_str).decode(“base64”)
@Solutionist将
b64_str
替换为包含字符串的数据变量。我得到以下
fixeddata=re.sub(“data:image/jpeg;base64',”,data.decode(“base64”))属性错误:“str”对象没有属性“decode”
抱歉
fixeddata=re.sub(“data:image/jpeg;base64”,“data”)。解码(“base64”)属性错误:“str”对象没有属性“decode”
尝试将数据转换为字节。
re.sub(“data:image/jpeg;base64”,“bytes(data))。解码(“base64”)