需求:统计图像数据集下图像分辨率的分布情况,以便将图像在输入到模型之前裁剪到合适的大小
待统计的图像数据:(位于./lid/
下)
代码:
import os
import pandas as pd
from PIL import Image
from collections import Counter
source = './lid/'
imgfile = os.listdir(source)
shot_cut = []
long_cut = []
print('filenum:', len([lists for lists in os.listdir(source)]))
for img in imgfile:
img_path = os.path.join(source, img)
img = Image.open(img_path)
imgSize = img.size # 图片的长和宽
#print(imgSize)
maxSize = max(imgSize) # 图片的长边
long_cut.append(maxSize)
minSize = min(imgSize) # 图片的短边
shot_cut.append(minSize)
#shot_result = Counter(shot_cut)
#long_result = Counter(long_cut)
shot_result = pd.value_counts(shot_cut, normalize=True)
long_result = pd.value_counts(long_cut, normalize=True)
print(shot_result)
print(long_result)
shot_result.to_csv('shot.csv', header=0)
long_result.to_csv('long.csv', header=0)
结果:
长边分布:
短边分布:
由于目前使用的模型要求输入长宽一致,根据分布情况可以裁剪成360*360
左右。