with open('1.jpg', 'rb') as f:
img = f.read()
如何将 img 转成 numpy 数组呢?
with open('1.jpg', 'rb') as f:
img = f.read()
如何将 img 转成 numpy 数组呢?
|
DustyposaAug 20, 2019
1
PIL can help you
(pip install Pillow) |
|
angles6645Aug 20, 2019
3
```
from PIL import Image import numpy as np img=np.array(Image.open('1.png')) ``` |
|
angles6645Aug 20, 2019 via Android
5
@epleone 那可以用 StringIO 转换一下 read 得到的
|
|
xuanhan863Aug 20, 2019
6
help(np.frombuffer)
|
|
7
@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
|