我有一個名為“p”的檔案夾,其中有 60 張 .png 影像,我需要將像素大小更改為那些不是 113 x 222 像素的影像
import cv2
import numpy as np
import glob
img_files = glob.glob("p/*.png")
for img_file in img_files:
if(): #The condition that verify the dimentions
img = cv2.imread(img_file)
res = cv2.resize(img, dsize=(113, 222), interpolation=cv2.INTER_CUBIC)
我認為使用 for 回圈我可以瀏覽該目錄中的影像,但我不確定如何驗證必須調整大小的影像的像素大小,哪些不應該。
重要的是,其余具有 113 x 222 像素的影像不要修改它們,而只用調整大小的版本替換以前沒有這些尺寸的影像。
uj5u.com熱心網友回復:
最后我可以制作這個代碼:
import cv2
import numpy as np
import glob
img_files = glob.glob("imgs/*.png")
for img_file in img_files:
print(img_file)
img = cv2.imread(img_file)
print(img.shape)
if( img.shape != (222, 113, 3) ):
print("Esta imagen no coincide con las dimensiones deseadas! La redimensionaré!")
res = cv2.resize(img, dsize=(113, 222), interpolation=cv2.INTER_CUBIC)
#cv2.imwrite(img_file, res) #Reemplaza por la nueva imagen ya redimensionada
print("Imagen: " str(img_file) " redimensionada " "--->" str(res.shape))
print("\n-------\n")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/378618.html
標籤:Python 蟒蛇-3.x 图片 opencv 图像大小调整
