我正在将图像转换为base64字符串,并将其从android设备发送到服务器。现在,我需要将该字符串更改为图像并将其保存在数据库中。
有什么帮助吗?
发布于 2013-04-25 12:06:31
试试这个:
import base64
imgdata = base64.b64decode(imgstring)
filename = 'some_image.jpg' # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
f.write(imgdata)
# f gets closed when you exit the with statement
# Now save the value of filename to your database
发布于 2018-01-02 06:19:37
将base64_string转换为opencv:
from PIL import Image
import cv2
# Take in base64 string and return cv image
def stringToRGB(base64_string):
imgdata = base64.b64decode(str(base64_string))
img = Image.open(io.BytesIO(imgdata))
opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
return opencv_img
发布于 2013-04-25 12:07:53
只需使用.decode('base64')
方法,就可以快乐了。
您还需要检测映像的mimetype/扩展,因为您可以正确保存它,在一个简短的示例中,您可以为django视图使用下面的代码:
def receive_image(req):
image_filename = req.REQUEST["image_filename"] # A field from the Android device
image_data = req.REQUEST["image_data"].decode("base64") # The data image
handler = open(image_filename, "wb+")
handler.write(image_data)
handler.close()
然后,根据您的需要使用保存的文件。
很简单。非常简单。;)
https://stackoverflow.com/questions/16214190
复制相似问题