with open('1.jpg', 'rb') as f:
img = f.read()
如何将 img 转成 numpy 数组呢?
1
Dustyposa 2019-08-20 17:12:39 +08:00
PIL can help you
(pip install Pillow) |
3
zst 2019-08-20 18:20:57 +08:00
```
from PIL import Image import numpy as np img=np.array(Image.open('1.png')) ``` |
6
xuanhan863 2019-08-20 19:38:11 +08:00
help(np.frombuffer)
|
7
epleone OP @zst @xuanhan863
谢谢两位 一张(480, 640, 3)的彩色图片, 我这么做可以读到图片 ```python im = np.array(Image.open(BytesIO(buffer))) ``` 但是使用 np.frombuffer 读不进来,这是为啥啊 ,感觉使用 np.frombuffer 会更优雅一些。 ``` python img = np.frombuffer(buffer, dtype=np.uint8) img.reshape(480, 640, 3) # 结果和上面一样 # img = np.frombuffer(buffer, dtype=np.uint8, count=480*640*3) ``` 错误提示是 ValueError: cannot reshape array of size 44263 into shape (480,640,3) |
8
epleone OP |