Python:从base64字符串转换为opencv的替代方法 [英] Python: Alternate way to covert from base64 string to opencv

查看:996
本文介绍了Python:从base64字符串转换为opencv的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此字符串转换为base64( http://pastebin.com/uz4ta0RL ) OpenCV可以使用的东西。
目前我使用此代码(img_temp是字符串)通过将OpenCV函数转换为临时图像(imagetosave.jpg)然后使用cv2.imread加载临时图像来使用它。

I'm trying to convert this string in base64 (http://pastebin.com/uz4ta0RL) to something usable by OpenCV. Currently I am using this code (img_temp is the string) to use the OpenCV functions by converting it to a temp image(imagetosave.jpg) then loading the temp image with cv2.imread.

    fh = open('imagetosave.jpg','wb')
    fh.write(img_temp.decode('base64'))
    fh.close()

    img = cv2.imread('imagetosave.jpg',-1)

虽然这种方法有效,但我希望能够在不使用临时图像的情况下使用OpenCV函数。
帮助将非常感谢,谢谢!

Though this method works, I would like to be able to use the OpenCV functions without using a temp image. Help would be much appreciated, thanks!

推荐答案

您可以将解码后的字符串转换为整数列表,然后你可以传递给 imdecode 函数:

You can convert your decoded string to a list of integers, which you can then pass to the imdecode function:

img_array = map(int, img_temp.decode('base64'))
img = cv2.imdecode(img_array, -1)

这篇关于Python:从base64字符串转换为opencv的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆