(from torchvision import transforms as T)
import numpy as np
from PIL import Image
from torchvision import transforms as T
# import matplotlib.image as Img
img = Image.open('4.jpg')
img_tran = T.Resize((512,256))(img)
print(img_tran.size) #(256, 512)
print(np.array(img_tran).shape) # (512, 256, 3)
img_to = T.ToTensor()(img_tran)
img_to_array = np.array(img_to)
print(img_to.size()) #torch.Size([3, 512, 256])
print(img_to_array.shape) # (3, 512, 256)
-T.Resize((H,W))(img)
np.array(img)
from PIL import Image
import numpy as np
img = Image.open('4.jpg')
img_array = np.array(img)
Image.fromarray(img_arr.astype(‘uint8’))
Image.fromarray(np.uint8(img))
from PIL import Image
import numpy as np
img = Image.open('4.jpg')
img_array = np.array(img)
img_image = Image.fromarray(img_arr.astype('uint8'))
Image.size属性显示的是**宽、高**
img_array.shape属性,显示的是**高、宽、通道**
from PIL import Image
from torchvision import transforms as T
import numpy as np
img = Image.open('4.jpg')
img_array = np.array(img)
print(img.size) # (720, 1160)
print(img_arr.shape) # (1160, 720, 3)
plt.figure()
plt.subplot(1,3,1)
plt.imshow(img)
plt.subplot(1,3,2)
plt.imshow(img_array)
plt.show()
功能:将读取后的图片尺寸的宽和高修改
import cv2
import matplotlib.image as Img
from torchvision import transforms as T
img = Image.open('4.jpg')
img_resize = img.resize((512,256))
img_tran = T.Resize((512,256))(img)
img_array = np.array(img)
img_read = cv2.imread('4.jpg')
img_cv2 = cv2.resize(img_read,(512,256))
print(img_resize.size) #(512, 256)
print(img_array.shape) #(1160, 720, 3)
print(img_tran.size) # (256, 512)
print(img_cv2.shape) # (256, 512, 3)
plt.figure()
plt.subplot(1,3,1)
plt.title('img_resize')
plt.imshow(img_resize)
plt.subplot(1,3,2)
plt.title('img_tran')
plt.imshow(img_tran)
plt.subplot(1,3,3)
plt.title('img_cv2')
plt.imshow(img_cv2)
plt.show()