欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > python 统计文件夹中图片尺寸

python 统计文件夹中图片尺寸

2025/2/23 22:43:20 来源:https://blog.csdn.net/qq_36784503/article/details/141936502  浏览:    关键词:python 统计文件夹中图片尺寸

python Code

import os
import matplotlib.pyplot as plt
import numpy as np
import copy
import shutil
import cv2
import matplotlib.pyplot as plt
def cv_imread(file_path):#imdedcode读取的是RGB图像cv_img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)return cv_imgdef statistic_w_h(images_names_path):heights_ = []widths_ = []for img_name in images_names_path:# print("images_names_path", images_names_path)if img_name[-4:] != "json":# print("img_name", img_name)img = cv_imread(img_name)# cv2.imshow("img",img)# cv2.waitKey(0)# print(img.shape)shape0 = img.shape[0]shape1 = img.shape[1]img_height = shape0img_width = shape1if img_width > img_height:img_height = shape1img_width = shape0heights_.append(img_height)widths_.append(img_width)return heights_, widths_def get_img_name(src_path_root_):image_paths = os.listdir(src_path_root_)print("-------------image_paths---------------", src_path_root_)images_names_all = []for image_path in image_paths:images_names = []abs_image_path = src_path_root_+'/'+image_path# print("abs_image_path", abs_image_path)images_name_ = os.listdir(abs_image_path)print("abs_image_path:", abs_image_path)print("Number", len(images_name_))# print("images_name_", images_name_)for img_name in images_name_:img_name = abs_image_path+'/'+img_name# print(img_name)images_names.append(img_name)images_names_all += images_namesreturn images_names_alldef get_image_name_list(paths_):names = []print("get_image_name_list:", paths_)for path in paths_:names += get_img_name(path)return namesif __name__ == "__main__":path_root_root = "E:/L208_part/"component_type = 'R'statistic_path_list = []root_paths = os.listdir(path_root_root)print(root_paths)for root_path in root_paths:abs_statistic_path = path_root_root+root_path+'/'+component_typestatistic_path_list.append(abs_statistic_path)print(statistic_path_list)images=get_image_name_list(statistic_path_list)print("len of image:", len(images))heights, widths = statistic_w_h(images)plt.hist(heights, bins=10, color='skyblue', alpha=0.5,label='heights')plt.hist(widths,  bins=10, color='green', alpha=0.5,label='widths')plt.title('strip class components -R ')plt.xlabel('Pixels')plt.ylabel('Frequency')plt.legend()plt.show()

2 目录结构

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词