resize圖片大小和reshape圖片形狀

1.reszie

img = cv2.imread('./img/'+file)
print(img.shape)
height, width = img.shape[:2]
size = (300,600)
shrink = cv2.resize(img, size, interpolation = cv2.INTER_AREA)
shrink2 = cv2.resize(img, (600,300))
aa = np.resize(img, (300,600))
img.resize((300,600))

用cv2resize函數:

cv2.reszie(img, (w,h))而不是cv2.reszie(img, (h,w))

所以假如現在一張圖片尺寸是(1080,1920)....(h,w) 就是1080行,1920列

你想把他變成600行,800列,(h,w)=(600, 800)

res = cv2.reszie(img, (800, 600)) ,插值方式默認,這纔是對的,因爲resize操作都是從先x方向在y方向

用PIL也是一樣的,resize參數也是(w,h)

img= Image.open(file)path)
img = img.resize([width, 32], Image.ANTIALIAS)

但是想用numpy的resize來改變圖片大小是沒有用的,不管是np.reszie還是直接img.resize,我看了下生成的圖片根本不是,全是亂來的像素點,以後切記。用cv2的resize函數吧

2,reshape

reshape可以直接用numpy的,

img.shape=(32,32)

img2 = img.reshape((-1,32,32,1))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章