獲取圖片大小功茴,并繪制成散點圖:
import time
import matplotlib.image as mping
import os
import matplotlib.pyplot as plt
#獲取單個圖片的大小
def get_Image_Size(image_Path):
#print("獲取圖片尺寸大小:"+ image_Path)
try:
lena = mping.imread(image_Path)
except:
time.sleep(0.5)
print("讀圖出錯侦锯,重讀")
getPngSize(image_Path)
size = tuple(lena.shape)
#print(size)
return size[1],size[0]
#遍歷目標(biāo)文件夾下圖片的大小,保存到list中
def get_dir_list(image_path):
w_list = []
h_list = []
files = os.listdir(image_path)
for i in files:
if i.endswith('.jpg'):
w,h = get_Image_Size(image_path + i)
#print(w,h)
w_list.append(w)
h_list.append(h)
return w_list,h_list
#畫散點圖
def plot_pic(w_list,h_list):
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_title('DataSets\' Size Analysis')
ax1.scatter(w_list, h_list, s=5, c='k', marker='.')
plt.show()
if __name__ == '__main__':
image_path = "./13/" #目標(biāo)文件夾澳淑,也就是圖片存放的目錄
w_list,h_list = get_dir_list(image_path)
plot_pic(w_list,h_list)
運行結(jié)果: