2020/01/19 19:59

图片转数组

import matplotlib.pyplot as plt

im_file='test_image.jpg'
print(img.shape)
print(img.dtype)
# img: numpy array with shape （H，W，c）
# uint8


数组转图片

三通道，浮点数组

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).


# float array, 3 channels
# For float array with 3 channels, by default the values out of range [0,1] are **Clipped** !
x=np.ones([500,600,3])
x*=0.4
for i in range(100,200):
x[i]=np.ones([600,3])*9
for i in range(250,300):
x[i]=np.ones([600,3])*-2
for i in range(370,400):
x[i]=np.ones([600,3])*0.7
print(x.dtype)
plt.imshow(x)
plt.axis('off')
plt.show()


单通道，浮点数组

# float array, 1 channel
# For float array with 1 channels, by default all values are normalized
x=np.ones([500,600])
x*=100
for i in range(100,200):
x[i]=np.ones([600])*200
print(x.dtype)
plt.imshow(x,cmap='gray')
plt.axis('off')
plt.show()


三通道，整形

# int array, 3 channels
x=np.ones([500,600,3])
x*=100
for i in range(100,200):
x[i]=np.ones([600,3])*900
for i in range(250,300):
x[i]=np.ones([600,3])*-2
for i in range(370,400):
x[i]=np.ones([600,3])*200
x=x.astype(np.int64)
print(x.dtype)
plt.imshow(x)
plt.axis('off')
plt.show()


单通道，整形

# int array, 1 channel
# For int array, by default the array range is mapped to [0,255].
x=np.ones([500,600])
x*=100
for i in range(100,200):
x[i]=np.ones([600])*175
x=x.astype(np.int64)
print(x)
print(x.dtype)
plt.imshow(x,cmap='gray')
plt.axis('off')
plt.show()


总结

matplotlib读取jpg图片时，默认是uint8类型的numpy数组。

0
0 收藏

0 评论
0 收藏
0