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

相关推荐
顾安r32 分钟前
11.20 开源APP
服务器·前端·javascript·python·css3
萧鼎1 小时前
Python PyTesseract OCR :从基础到项目实战
开发语言·python·ocr
sali-tec2 小时前
C# 基于halcon的视觉工作流-章62 点云采样
开发语言·图像处理·人工智能·算法·计算机视觉
没有bug.的程序员2 小时前
Java 字节码:看懂 JVM 的“机器语言“
java·jvm·python·spring·微服务
gorgeous(๑>؂<๑)3 小时前
【ICLR26匿名投稿】Context-Aware ViT:让目标检测真正“看清上下文”的增强策略
人工智能·目标检测·机器学习·计算机视觉·目标跟踪
张人玉3 小时前
OCR 字符识别助手详解(含 Halcon 示例)
人工智能·机器学习·计算机视觉·halcon
下午见。4 小时前
Python基础入门:用Anaconda搭建环境的启蒙之旅
python
我叫汪枫5 小时前
Python 办公自动化入门:玩转 Excel 与 Word
python·word·excel
E_ICEBLUE5 小时前
三步完成 Markdown 到 Word/PDF 的转换:Python 教程
python·pdf·word·markdown·格式转换
AI浩5 小时前
Cambrian-S:迈向视频中的空间超感知
人工智能·目标检测·计算机视觉·音视频