python 统计文件夹中图片尺寸

python Code

python 复制代码
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_img


def 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 = shape0
            img_width = shape1
            if img_width > img_height:
                img_height = shape1
                img_width = shape0
            heights_.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_names
    return images_names_all


def get_image_name_list(paths_):
    names = []
    print("get_image_name_list:", paths_)
    for path in paths_:
        names += get_img_name(path)
    return names


if __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_type
        statistic_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 目录结构

相关推荐
FreakStudio38 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal2 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali2 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ2 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.3 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~3 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算
爆更小小刘3 小时前
Python基础语法(3)下
开发语言·python
哪 吒3 小时前
华为OD机试 - 第 K 个字母在原来字符串的索引(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od
憨憨小白4 小时前
Python 的集合类型
开发语言·python·青少年编程·少儿编程
白杆杆红伞伞4 小时前
01_快速入门
python·pandas