功能:将图片大小统一成720p。
import os
import io
import math
import sys
import cv2
import shutil
import random
import numpy as np
from collections import namedtuple, OrderedDict
def get_files(dir, suffix):
res = []
for root, directory, files in os.walk(dir):
for filename in files:
name, suf = os.path.splitext(filename)
if suf in suffix:
#res.append(filename)
res.append(os.path.join(root, filename))
return res
def uniform_image_size(list_path,width_size,height_size):
image_list = get_files(list_path, ['.jpg'])
total_len = len(image_list)
print('total_label_len', total_len)
for i in range(0, total_len):
image_file = image_list[i]
img = cv2.imread(image_file)
if img.shape[0] != height_size or img.shape[1] != width_size:
img = cv2.resize(img, (width_size,height_size), interpolation=cv2.INTER_LINEAR)
os.remove(image_file)
cv2.imwrite(image_file, img)
print('img.shape[0]',img.shape[0],'--------',image_file)
random.shuffle(image_list)
def main():
list_path = r'E:\projection\fair\data\genarate_4to1\negative_image\101'
width_size = 1280
height_size = 720
uniform_image_size(list_path,width_size,height_size)
if __name__ == '__main__':
main()