将图像字节数据流解码为JPEG [英] Decode image bytes data stream to JPEG
问题描述
我正在努力从字节成功解码JPEG图像,然后再次解码回JPEG.
我从MJPG字节流的编码帧开始,我想对其进行解码以便使用OpenCV进行处理.我是Python,numpy,opencv等的新手!
我现在将文本文件中的JPG帧数据保存为:b'\ xf \ xd8 \ xff \ xdb \ x00 .... etc等,用于测试:
当我尝试在第14行(npFlat.reshape((640,480)))将numpy数组的大小调整为原始视频流分辨率(640,480)时,代码似乎失败.** ValueError:无法将大小为228140的数组重塑为形状(640,480)*
import io
import cv2
import numpy as np
BytesFile = open('FrameBytes.txt')
MyBytes=BytesFile.read()
BytesFile.close()
dt=np.dtype(np.unit8)
dt=dt.newbtyeorder('>')
npFlat = np.fromfile('FrameBytes.txt'.dtype=dt)
npResized = npFlat.reshape(640,480,3) #CODE FAILING TO RESIZE AT THIS LINE...
cv.imshow('resized',npResized)
即使是从640、480提要中捕获了我的视频帧,还是由于某种原因,在编码过程中大小发生了变化?这是我目前所能想到的.任何帮助都欢迎.
我已经查看了相关的文章: Python-NumPy的字节图像OpenCV 使用数组但尝试避免使用PIL,并且frombuffer方法对我来说似乎也失败了.
好,所以我取得了一些进展,现在有了:
npFlat = np.frombuffer(MyBytes.encode('utf-8'),dtype=np.int8).
当我将npFlat重塑为(374,610)时,我现在还可以获得成功的代码. IE.因此374 x 610 =平面numpy数组,长度为228140 ...但是,这一切似乎都很奇怪.缓冲区信息表示一个JPG,我正尝试重新打开它...但尚未关闭.
MyBytes.txt"数据输入文件在此处可见: https://drive.google.com/file/d/18pqILl9myeTRjdiqtExFyOe94Km_ /view?usp = sharing] 1
您已经一团糟-您应该避免将二进制数据另存为文本文件!
将文件内容复制到剪贴板中-在Mac上,命令为:
cat frame.txt | pbcopy
启动Python,并创建一个名为s
的变量,并将其设置为剪贴板的内容:
s = PASTE_YOUR_CLIPBOARD
现在做:
from PIL import Image
from io import BytesIO
# Load image from BytesIO
im = Image.open(BytesIO(s))
# Display image and save image
im.show()
im.save('result.png')
如果您使用的是 OpenCV ,请使用:
import cv2
# Make s as above
s = PASTE_YOUR_CLIPBOARD
i = np.frombuffer(s,dtype=np.uint8)
im = cv2.imdecode(i,cv2.IMREAD_UNCHANGED)
cv2.imwrite('result.png',im)
I am struggling to successfully decode a JPEG image from bytes, back to JPEG again.
I started from encoded frame from a MJPG bytes stream, which I want to decode in order to manipulate with OpenCV. I am a bit of a newbie at Python, numpy, opencv etc!
I now have the frame JPG data in a text file as: b'\xf\xd8\xff\xdb\x00....etc etc for purposes of testing:
code seems to fail when I try to Resize the numpy array to the original video stream resolution (640, 480) on line 14 (npFlat.reshape((640,480))
**ValueError: cannot reshape array of size 228140 into shape (640,480)*
import io
import cv2
import numpy as np
BytesFile = open('FrameBytes.txt')
MyBytes=BytesFile.read()
BytesFile.close()
dt=np.dtype(np.unit8)
dt=dt.newbtyeorder('>')
npFlat = np.fromfile('FrameBytes.txt'.dtype=dt)
npResized = npFlat.reshape(640,480,3) #CODE FAILING TO RESIZE AT THIS LINE...
cv.imshow('resized',npResized)
Could it be that even though my video frame was captured from a 640, 480 feed, for some reason during encoding the size has changed? This is all I can think of at the moment. Any/all help welcome.
I have reviewed a related post:Python - byte image to NumPy array using OpenCV but trying to avoid PIL, and the frombuffer method also seems to be failing for me.
Ok, so I made some progress and now have:
npFlat = np.frombuffer(MyBytes.encode('utf-8'),dtype=np.int8).
I can now also get the code to succeed when I 'reshape' npFlat to (374, 610). I.e. so that 374 x 610 = the flat numpy array, which is of length 228140...but this all seems odd. the buffer information represents a JPG which I am trying to reopen...and am not getting close yet.
MyBytes.txt" Data Input File is viewable here: https://drive.google.com/file/d/18pqILl9myeTRjdiqtExFyOe94Km_aNNM/view?usp=sharing]1
You have made quite a mess - you should avoid saving binary data as text files!
Copy the contents of your file into your clipboard - on a Mac the command is:
cat frame.txt | pbcopy
Start Python and make a variable called s
and set it to the contents of the clipboard:
s = PASTE_YOUR_CLIPBOARD
Now do:
from PIL import Image
from io import BytesIO
# Load image from BytesIO
im = Image.open(BytesIO(s))
# Display image and save image
im.show()
im.save('result.png')
If you are on OpenCV, use:
import cv2
# Make s as above
s = PASTE_YOUR_CLIPBOARD
i = np.frombuffer(s,dtype=np.uint8)
im = cv2.imdecode(i,cv2.IMREAD_UNCHANGED)
cv2.imwrite('result.png',im)
这篇关于将图像字节数据流解码为JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!