简体   繁体   English

我无法从字节读取带有 open-cv 的图像

[英]I can't read image with open-cv from bytes

Hey guys help me please.嘿伙计们请帮助我。

I have flask rest api that receive multipart/form-data as image and I want to use opencv to process it .我有flask rest api,它接收multipart/form-data 作为图像,我想使用opencv 来处理它。

My problem is I can't read image with cv2.imdecode.我的问题是我无法使用 cv2.imdecode 读取图像。

This function is return none so what's wrong.这个函数是 return none 所以有什么问题。

imgFile = request.files['image']
imgBuffer = imgFile.read() # <class 'bytes'>
#b'\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10'

img = np.frombuffer(imgBuffer, dtype='uint8') # <class 'numpy.ndarray'>
#[195 191 195 152 195 191 195 160   0  16]

img = cv2.imdecode(img, cv2.IMREAD_COLOR) # <class 'NoneType'>

我的头像在这里

cv2.imdecode() expects to decode a JPEG-encoded or PNG-encoded image. cv2.imdecode()期望解码 JPEG 编码或 PNG 编码的图像。 Such an image would start with:这样的图像将从以下内容开始:

  • the JPEG signature ff d8 ff or JPEG 签名ff d8 ff
  • the PNG signature 89 50 4e 47 0d 0a 1a 0a PNG 签名89 50 4e 47 0d 0a 1a 0a

Yours does not.你的没有。 So it is probably just the raw, unencoded pixels and you probably just need:所以它可能只是原始的、未编码的像素,你可能只需要:

img = np.array(imgBuffer).reshape((height,width))

Another clue is the size of your bytes buffer.另一个线索是bytes缓冲区的大小。 If its size matches the height x width of your greyscale image (or 3x that if colour) it means your image is just pixel data, whereas you would expect a JPEG/PNG encoded image to be much smaller because it's compressed.如果它的大小与您的灰度图像的高度 x 宽度匹配(或 3x,如果是彩色),则意味着您的图像只是像素数据,而您希望 JPEG/PNG 编码的图像要小得多,因为它是压缩的。

Try making the following changes to your code.尝试对您的代码进行以下更改。

imgFile = request.files['image']
imgBuffer = imgFile.read() # <class 'bytes'>
#b'\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10'
my_img = cv2.imread(imgFile, 0)
my_img.imshow(my_img)
#stream = io.StringIO(imgBuffer.decode("UTF8"), newline=None)

#img = np.frombuffer(stream, dtype='uint8') # <class 'numpy.ndarray'>
#[195 191 195 152 195 191 195 160   0  16]

#img = cv2.imdecode(img, cv2.IMREAD_COLOR) # <class 'NoneType'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 RasberryPi Python 上安装 Open-CV - Can't install Open-CV on RasberryPi Python 为什么 Kaggle 不支持 imshow() ? 或者如何在kaggle中使用open-cv显示图像 - Why Kaggle doesn't suppot imshow() ? Or how to display image using open-cv in kaggle 我该怎么做才能安装 Open-CV 库? 如图所示,我遇到了冲突问题 - what can I do to install Open-CV library ? I'm having a conflict problem as shown in the picture 如何使用python open-cv获取图像名称 - how get image name by using python open-cv docker 图像中的 open-cv 安装在树莓派上不起作用 - open-cv installation in docker image does not work on raspberry pi 如何在python中找到open-cv摄像机校准常数,OpenCV错误:cv :: calibrateCamera中的断言失败(nimages&gt; 0) - How can I find open-cv camera calibration constant in python , OpenCV Error: Assertion failed (nimages > 0) in cv::calibrateCamera 打开 CV 无法从 URL 读取图像 - Open CV cant read an image from URL 如何用 open-cv 和 Picamera 捕捉人脸? - How to capture face with open-cv and Picamera? Python-如何使用Open-CV或PIL将24位PNG图像转换为32位 - Python - how to convert a 24-bit PNG image to 32-bit using Open-cv or PIL 使用 Open-CV 和 Kreas 将图像转换为数组,给出不同的 output - Converting image to array using Open-CV and Kreas giving different output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM