美文网首页
图片大小调整

图片大小调整

作者: 逍遥_yjz | 来源:发表于2020-10-16 15:41 被阅读0次
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)

相关文章

网友评论

      本文标题:图片大小调整

      本文链接:https://www.haomeiwen.com/subject/rzwzpktx.html