python学习(2)显示/保存/转换图片

显示、保存

matplotlib

dirpath = './image/1.jpg'

import matplotlib.pyplot as plt
img_plt = plt.imread(dirpath)

plt.imshow(img_plt, cmap=plt.cm.binary)
plt.show()

plt.imshow(img_plt[:,:,1], cmap=plt.cm.binary)
plt.show()

digit = [[135,26,33,12],[14,27,43,190],[120,124,134,205]]
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()
plt.savefig('new.jpg')

skimage.io

from skimage import io,data,data_dir
print(data_dir)
img1 = io.imread(dirpath)
io.imshow(img1)
io.show()
img2 = data.astronaut()
io.imshow(img2)
io.show()
io.imsave('./image/astronaut.jpg',img2)   #绝对路径与相对路径均可以

print(img2.size, img2.max(), img2.min(), img2.mean())
D:\Python\python38\lib\site-packages\skimage\data
786432 255 0 114.59900410970052

PIL

from PIL import Image
img = Image.open(dirpath)
img.show()
img.save('1new.jpg')

RGB与灰度转换

matplotlib

dirpath = './image/1.jpg'

import matplotlib.pyplot as plt
import numpy as np
img_plt = plt.imread(dirpath)

plt.imshow(img_plt, cmap=plt.cm.binary)
plt.show()

# plt.imshow(img_plt[:,:,1], cmap=plt.cm.binary)
# plt.show()
#
# digit = [[135,26,33,12],[14,27,43,190],[120,124,134,205]]
# plt.imshow(digit, cmap=plt.cm.binary)
# plt.show()

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])  #根据需求将三原色按比例灰度

img_gray = rgb2gray(img_plt)
plt.imshow(img_gray, cmap='Greys_r')
plt.axis('off')
plt.show()

PIL

dirpath = './image/1.jpg'
from PIL import Image
img = Image.open(dirpath)
imgL = img.convert('L')
imgL.show()

图像与array(numpy数组)的转化

由之前图片读取可知,除PIL读取的图像格式为非数组格式,其余的均为数组。

PIL非数组转化

dirpath = './image/1.jpg'
from PIL import Image
import numpy as np
img_pil = Image.open(dirpath)  #读取的非numpy.ndarray格式
print('img_pil1:',img_pil)
print('img_pil1:',type(img_pil))
img_pil = np.array(img_pil)    #格式转换

数组转化为PIL图像

import matplotlib.image as mpimg
from PIL import Image
import numpy as np
img = mpimg.imread(dirpath)
print(img)
img = Image.fromarray(img)  #np.uinit8(img)
print(img)
img.show()

PIL.Image/numpy.ndarray与Tensor的相互转换

import numpy as np
import cv2
import torch
from torchvision import transforms
img_path = “” # here is your img path
transform = transforms.Compose([
transforms.ToTensor(), # convert range [0, 255] to range [0, 1]
])

ndarray -> tensor

img = cv2.imread(img_path)
print(type(img)) # <class ‘numpy.ndarray’>
print(img.shape) # (300, 300, 3) H, W, C
img1 = transform1(img)
print(type(img1)) # <class ‘torch.Tensor’>
print(img1.shape) # torch.Size([3, 300, 300]) C, H, W
print(“img max value :”,np.max(img), " img1 max value:", torch.max(img1)) # img max value : 255 img1 max value: tensor(1.)

PIL.Image -> tensor

from PIL import Image
img3 = Image.imread(img_path)
print(type(img3)) #<class ‘PIL.Image.Image’>
img4 = transform1(img3)
print(type(img4)) # <class ‘torch.Tensor’>
print(img4.shape) # torch.Size([3, 300, 300]) C, H, W
img3.show() # present the img
print(“img3 max value :”,np.max(img4), " img4 max value:", torch.max(img4)) # img3 max value : 255 img4 max value: tensor(1.)

tensor -> PIL.Image

transform2 = transforms.Compose([transforms.ToPILImage()])
img5 = transform2(img4)
img5.show()

猜你喜欢

转载自blog.csdn.net/weixin_42535423/article/details/117848976