批量修改图片大小

有时候我们收集来的图片数据分辨率过高会导致卷积计算时内存不够用,就需要们提前处理把图片分辨率降低到合适的分辨率。

import cv2 
import matplotlib.pyplot as plt
import numpy as np

def show(image):
    plt.imshow(image)
    plt.axis('off')
    plt.show()

def imread(image):
    image = cv2.imread(image)
    image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    return image
import os
import imghdr

imgType_list = {'jpg','bmp','png','jpeg','rgb','tif'}

def resize_img(path):
    directory = path+'/new'
    if not os.path.exists(directory):
        os.makedirs(directory)
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if file.endswith(".jpg"):
            image = imread(file_path)
            img_rs = cv2.resize(image, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA)
            img_rs = cv2.cvtColor(img_rs,cv2.COLOR_BGR2RGB)
            print(img_rs.shape)
            cv2.imwrite(path+'/new/'+file,img_rs,[int(cv2.IMWRITE_JPEG_QUALITY),100])
            #show(img_rs)
    print('缩小1/4完成')
path = r'E:\\dataset\\cup\test'
resize_img(path)