Python Pillow(PIL 第三方模塊)和 cv2 (opencv第三方模塊)對圖片的 resize 操作 (縮放圖片大小)


PIL 模塊的 resize  操作:

 

 

1.  從文件中讀取圖片,然后  resize  大小:

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

img=Image.open(r"1.jpg")
print("原圖的height,weight分別為:", np.asarray(img).shape[:2])
plt.imshow(np.asarray(img))
plt.show()

height, weight = (np.asarray(img).shape)[:2]
height = height//10
weight = weight//10
img2 = Image.Image.resize(img, (weight, height))
print("resized后圖的height,weight分別為:", np.asarray(img2).shape[:2])
plt.imshow(np.asarray(img2))
plt.show()

 

 

 

 

 

2.  從字節碼(Bytes)中讀取圖片,然后  resize  大小:

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from io import BytesIO


img = open("1.jpg", "rb").read()   #讀取序列化的二進制碼
img = BytesIO( img )
img = Image.open( img )
print("原圖的height,weight分別為:", np.asarray(img).shape[:2])
plt.imshow(np.asarray(img))
plt.show()

height, weight = (np.asarray(img).shape)[:2]
height = height//10
weight = weight//10
img2 = Image.Image.resize(img, (weight, height))
print("resized后圖的height,weight分別為:", np.asarray(img2).shape[:2])
plt.imshow(np.asarray(img2))
plt.show()

 

 

 

---------------------------------------------------

 

 

 

 

CV2 模塊的 resize  操作:

 

 

 

讀入圖像

使用函數cv2.imread()來讀取圖像。圖像應該在工作目錄中,或者應該給出圖像的完整路徑。

imread(filename[, flags]) -> retval

函數imread從指定文件加載圖像並返回一個numpy.ndarray對象類型像素值。 如果圖像無法讀取(由於文件丟失,權限不當,格式不受支持或格式無效),函數返回一個空矩陣

第二個參數是一個標志,用於指定應讀取圖像的方式。

  • cv2.IMREAD_COLOR:加載彩色圖像。圖像的任何透明度都將被忽略。這是默認標志。 flags=1
  • cv2.IMREAD_GRAYSCALE:以灰度模式加載圖像 flags=0
  • cv2.IMREAD_UNCHANGED:加載包含Alpha通道的圖像 flags=-1

 

注意

而不是這三個標志,你可以簡單地傳遞整數1,0或-1。

 

取自於:  https://blog.csdn.net/hubingshabi/article/details/80144706

 

 

 

CV2 讀取圖片,  CV2展示圖片:

import matplotlib.pyplot as plt
import numpy as np
import cv2
# rgb圖
img=cv2.imread(r"1.jpg", 1)
# 灰度圖
#img=cv2.imread(r"1.jpg", 0)

print("原圖的height,weight分別為:", np.asarray(img).shape[:2])
#plt.imshow(np.asarray(img))
#plt.show()
cv2.imshow("img", mat=img)
cv2.waitKey (0)


height, weight = (img.shape)[:2]
height = height//3
weight = weight//3
img2 = cv2.resize(img, (weight, height))
print("resized后圖的height,weight分別為:", img2.shape[:2])
#plt.imshow(np.asarray(img2))
#plt.show()
cv2.imshow("img2", mat=img2)
cv2.waitKey (0)
cv2.destroyAllWindows()

 

 

 

 

CV2 讀取圖片,  matplotlib展示圖片:   把cv2的bgr轉換為rgb,然后展示。       [...,::-1]

import matplotlib.pyplot as plt
import numpy as np
import cv2
# rgb圖
img=cv2.imread(r"1.jpg", 1)
# 灰度圖
#img=cv2.imread(r"1.jpg", 0)

print("原圖的height,weight分別為:", np.asarray(img).shape[:2])
plt.imshow(np.asarray(img)[...,::-1])
plt.show()


height, weight = (img.shape)[:2]
height = height//3
weight = weight//3
img2 = cv2.resize(img, (weight, height))
print("resized后圖的height,weight分別為:", img2.shape[:2])
plt.imshow(np.asarray(img2)[...,::-1])
plt.show()

 

 

 

-----------------------------------------------------------------

 

 

參考網址:

https://blog.csdn.net/sinat_26917383/article/details/78559709

https://blog.csdn.net/hubingshabi/article/details/80144706

 

 

----------------------------

 

 

 

 

 1.jpg

 

 

注:上面的代碼均使用該圖片(1.jpg)做測試。

 

 

 

 

--------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM