图片大小调整

import cv2

def img_resize(image):
    height, width = image.shape[0], image.shape[1]
    # 设置新的图片分辨率框架
    width_new = 600
    height_new = 800
    # 判断图片的长宽比率
    if width / height >= width_new / height_new:
        img_new = cv2.resize(image, (width_new, int(height * width_new / width)))
    else:
        img_new = cv2.resize(image, (int(width * height_new / height), height_new))
    return img_new

import os
path = 'C:/Users/Desktop/moreImage/test'
path2 = 'C:/Users/Desktop/moreImage/result'
fileList = os.listdir(path)


for file in fileList:

    path1 = os.path.join(path + '/', file)
    print(path1)

    img = cv2.imread(path1)

    a = img.shape

    print(a)
    # im2 = cv2.resize(img,(600,800),)  # 为图片重新指定尺寸
    im2 = img_resize(img)
    new_path = os.path.join(path2 + '/', file)
    #cover_path = r'C:/Users/yuhai/Desktop/1234.jpg'
    cv2.imwrite(new_path,im2)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容