Mar*_*iak 8 python opencv numpy python-3.x
我有一个以字节为单位的图像:
print(image_bytes)
b'\xff\xd8\xff\xfe\x00\x10Lavc57.64.101\x00\xff\xdb\x00C\x00\x08\x04\x04\x04\x04\x04\x05\x05\x05\x05\x05\x05\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x07\x07\x07\x08\x08\x08\x07\x07\x07\x06\x06\x07\x07\x08\x08\x08\x08\t\t\t\x08\x08\x08\x08\t\t\n\n\n\x0c\x0c\x0b\x0b\x0e\x0e\x0e\x11\x11\x14\xff\xc4\x01\xa2\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\ ... some other stuff
我可以使用将其转换为NumPy数组Pillow
:
image = numpy.array(Image.open(io.BytesIO(image_bytes)))
Run Code Online (Sandbox Code Playgroud)
但是我真的不喜欢使用枕头。有没有办法使用清晰的OpenCV,或者直接使用更好的NumPy,或者使用其他更快的库?
Nor*_*ius 12
我创建了一个2x2 JPEG图像进行测试。图像具有白色,红色,绿色和紫色像素。我曾经cv2.imdecode
和numpy.frombuffer
import cv2
import numpy as np
f = open('image.jpg', 'rb')
image_bytes = f.read() # b'\xff\xd8\xff\xe0\x00\x10...'
decoded = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1)
print('OpenCV:\n', decoded)
# your Pillow code
import io
from PIL import Image
image = np.array(Image.open(io.BytesIO(image_bytes)))
print('PIL:\n', image)
Run Code Online (Sandbox Code Playgroud)
尽管通道顺序是BGR而不是RGB,但这似乎可行PIL.Image
。您可能会使用一些标志来进行调整。检测结果:
OpenCV:
[[[255 254 255]
[ 0 0 254]]
[[ 1 255 0]
[254 0 255]]]
PIL:
[[[255 254 255]
[254 0 0]]
[[ 0 255 1]
[255 0 254]]]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8699 次 |
最近记录: |