【numpy】矩阵和图片的相互转化

from PIL import Image
import numpy as np

x=np.load("universal.npy")  # x.shape=m*n*3
x=(x-np.min(x))*255/np.max(x)

im=Image.fromarray(np.uint8(x))

im.show()
im.save('flag.jpg')
from PIL import Image
import numpy as np

im = Image.open(r'test.jpeg')			# im是Image对象
img = np.array(im)						# img是h×w×3的ndarray (0-255,RGB)
img = np.asarray(im)					# img是h×w×3的ndarray (0-255,RGB)
'''
https://github.com/numpy/numpy/blob/bcbed877f42ed6e9b01b2125134db4b6395f1d9d/numpy/core/numeric.py#L413-481
asarray<=>array(copy=false)
https://stackoverflow.com/questions/14415741/numpy-array-vs-asarray
'''

np.save('test.npy',img)

你可能感兴趣的:(CV,python)