OpenCV:图像尺寸伸缩resize

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u013419318/article/details/102544274
import cv2
import os
import matplotlib.pylab as plt

# 缩小图像
def shrink_DirImages(source_path,save_path):
    if not os.path.exists(source_path):
        return
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    sour_path = source_path
    sav_path = save_path
    for i in range(1,100):
        source_path = os.path.join(source_path,str(i))
        save_path = os.path.join(save_path,str(i))
        for file in os.listdir(source_path):
            img = plt.imread(os.path.join(source_path,file), 0)
            print(file)
            # if img == None:
            #     "Error: could not load image"
            #     os._exit(0)
            # height, width = img.shape[:2]
            size = (int(1024), int(1024))
            shrink = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
            cv2.imwrite(os.path.join(save_path,file),shrink)
        source_path = sour_path
        save_path = sav_path

# def shrink_Images(source_path,save_path):
#     if not os.path.exists(source_path):
#         return
#     if not os.path.exists(save_path):
#         os.makedirs(save_path)
#     for file in os.listdir(source_path):
#         img = plt.imread(os.path.join(source_path, file), 0)
#         print(file)
# 
#         size = (int(512), int(512))
#         # size = (int(1024), int(1024))
#         shrink = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
#         cv2.imwrite(os.path.join(save_path, file), shrink)


# 放大图像
def amplification_image():
    fx = 1.6
    fy = 1.2
    # enlarge = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_CUBIC)

if __name__ == '__main__':
    source_path = 'H:\\0\\test'
    shrink_Images(source_path,source_path)

猜你喜欢

转载自blog.csdn.net/u013419318/article/details/102544274