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 小时前
【计算机视觉基础CV-图像分类】01- 从历史源头到深度时代:一文读懂计算机视觉的进化脉络、核心任务与产业蓝图
人工智能·计算机视觉
黄公子学安全1 小时前
Java的基础概念(一)
java·开发语言·python
程序员一诺2 小时前
【Python使用】嘿马python高级进阶全体系教程第10篇:静态Web服务器-返回固定页面数据,1. 开发自己的静态Web服务器【附代码文档】
后端·python
小木_.2 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器
小陈phd2 小时前
OpenCV学习——图像融合
opencv·计算机视觉·cv
Jiude3 小时前
算法题题解记录——双变量问题的 “枚举右,维护左”
python·算法·面试
唐小旭3 小时前
python3.6搭建pytorch环境
人工智能·pytorch·python
是十一月末3 小时前
Opencv之对图片的处理和运算
人工智能·python·opencv·计算机视觉
爱学测试的李木子3 小时前
Python自动化测试的2种思路
开发语言·软件测试·python
kitsch0x974 小时前
工具学习_Conan 安装第三方库
开发语言·python·学习