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 目录结构

相关推荐
鸡鸭扣1 小时前
Docker:3、在VSCode上安装并运行python程序或JavaScript程序
运维·vscode·python·docker·容器·js
paterWang2 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
东方佑2 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
神秘_博士3 小时前
自制AirTag,支持安卓/鸿蒙/PC/Home Assistant,无需拥有iPhone
arm开发·python·物联网·flutter·docker·gitee
Moutai码农4 小时前
机器学习-生命周期
人工智能·python·机器学习·数据挖掘
小白教程5 小时前
python学习笔记,python处理 Excel、Word、PPT 以及邮件自动化办公
python·python学习·python安装
武陵悭臾5 小时前
网络爬虫学习:借助DeepSeek完善爬虫软件,实现模拟鼠标右键点击,将链接另存为本地文件
python·selenium·网络爬虫·pyautogui·deepseek·鼠标右键模拟·保存链接为htm
代码猪猪傻瓜coding5 小时前
关于 形状信息提取的说明
人工智能·python·深度学习
码界筑梦坊7 小时前
基于Flask的第七次人口普查数据分析系统的设计与实现
后端·python·信息可视化·flask·毕业设计
C#Thread7 小时前
机器视觉--索贝尔滤波
人工智能·深度学习·计算机视觉