如何正确读取 jpeg 二进制文件(不解码)并保存?

How can I read jpeg file binary (without decoding) properly and save it?

实际上,当我卡在正确加载图像以便能够读回它们的阶段时,我想将不解码(以保存位置)的 JPEG 图像插入数据帧 (pandas)与 PIL.

from PIL import Image
with open(fname, "rb") as f:
data = f.read()
img = Image.frombytes('RGB',d,data)

img = np.asarray(map(ord,img))

我在上面使用 ord() 找到了解决方案,但速度有点慢。 有没有更快的方法?

问题已经解决,谢谢!

您可以使用 mahotas 这是一个流行的库来 reading/quantifying 图像。通过终端 pip install mahotas.

安装
import mahotas as mh
import numpy as np

original_img = np.array(mh.imread('figure1.jpg'), dtype=np.float64) / 255
# check dimension, RGB
original_img.shape
Out[13]: (1536, 2048, 3)

width, height, depth = original_img.shape
# reshape 
image_flattened = np.reshape(original_img, (width*height, depth))

image_flattened.shape
Out[17]: (3145728, 3)