python 裁剪图片;位深度不变

裁剪图片,20562124->512512
先将图片缩小4倍,变为531514;然后提取前512个像素,变为512512

#裁剪图片,使图片变为512*512
import os
import cv2
import imutils
from PIL import Image

def resize_crop_image(image):
    (h, w) = image.shape[:2]
    image = imutils.resize(image, width=531,inter=cv2.INTER_AREA)
    (h, w) = image.shape[:2]
    image = image[0:512, 0:512]
    return cv2.resize(image, (512, 512),interpolation=cv2.INTER_AREA)

if __name__ =="__main__":
#读取固定文件夹下的所有图片
    filePath = 'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Non-Glaucoma/'
    savePath = 'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Non-Glaucoma_512/'
    name = os.listdir(filePath)
    for i in name:
        image_path = os.path.join(filePath,i)
        image_save_Path = os.path.join(savePath,i)
        #cv2.IMREAD_ANYCOLOR很重要,不会改变图片的位深度
        image = cv2.imread(image_path, cv2.IMREAD_ANYCOLOR)
        img = resize_crop_image(image)
        cv2.imwrite(image_save_Path, img)

你可能感兴趣的:(python,图片处理,深度学习)