1. Read the image
Use matplotlib. image
from matplotlib import image as mping
from matplotlib import pyplot as plt
import numpy as np
img=mping.imread('美女.jpg')#image.read()
plt.imshow(img)#图片显示
plt.show()#画布显示
print(type(img))
print(img.shape)
Use opencv
import cv2
from matplotlib import pyplot as plt
import numpy as np
img=cv2.imread('meinv (2).jpg')#
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(type(img))
print(img.shape)
import cv2
from matplotlib import pyplot as plt
import numpy as np
img=cv2.imread('meinv (2).jpg')#
plt.imshow(img)
plt.show()
print(type(img))
print(img.shape)
opencv saves BGR.matplotlib saves RGB
If you want to display as RGB normally, you need to add
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#BGR-RGB
Use PIL
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
img=Image.open('meinv (2).jpg')#
plt.imshow(img)
plt.show()
print(type(img))
print(img.shape)
The picture read by PIL is not in numpy format
Need for manual conversion
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
img=Image.open('meinv (2).jpg')#
img=np.array(img)
plt.imshow(img)
plt.show()
print(type(img))
print(img.shape)
2. Zoom image
PIL zoom
from PIL import Image,ImageOps
from matplotlib import pyplot as plt
import numpy as np
img=Image.open('meinv (2).jpg')#
img=np.array(img)#保存为numpy
img1=Image.fromarray(img)#读取numpy格式
print(img1.size)#(670, 419)
#缩放
img2=img1.resize((200,200))
print(img2.size)#(200, 200)
fig=plt.figure(figsize=(12,12))
a=fig.add_subplot(2,1,1)
imgplot=plt.imshow(img1)
a.set_title('before')
a=fig.add_subplot(2,1,2)
imgplot=plt.imshow(img2)
a.set_title('after')
plt.show()
Method two use
numpy.resize() method three use cv2.resize()
The syntax format
cv2.resize() has many parameters, among which src and dsize must be
cv2.resize(src.dsize)
src original image path
dsize target image size (column, row)
cv2.resize(src.dsize, fx, fy)
fx, fy zoom size ratio, when dsize is not used.
b=cv2.resize(a,dszie=None,fx=0.5,fy=0.5)
import cv2
import numpy as np
a=cv2.imread("image\\lenacolor.png")
b=cv2.resize(a,None,fx=1.2,fy=0.5)
cv2.imshow("original",a)
cv2.imshow("resize",b)
cv2.waitKey()
cv2.destroyAllWindows()