File size: 2,748 Bytes
6776f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Count Images in Folder\n",
    "\n",
    "---\n",
    "\n",
    "This script counts the total number of JPEG and PNG images in a specified directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total number of images: 21891\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "import glob\n",
    "\n",
    "\"\"\"\n",
    "This script counts the total number of JPEG and PNG images in a specified directory.\n",
    "\n",
    "The script uses the `glob` module to find all files with .jpg, .jpeg, and .png extensions\n",
    "within the given directory and its subdirectories. It then calculates the total count of these\n",
    "image files and prints the result.\n",
    "\n",
    "Functions:\n",
    "    count_images(directory): Counts and returns the number of image files in the directory.\n",
    "\n",
    "Usage:\n",
    "    To use this script, simply set the `directory_path` variable to the path of the directory\n",
    "    you want to process. Then run the script, and it will output the total number of images found.\n",
    "\n",
    "Note:\n",
    "    Ensure that the directory path uses double backslashes (\\\\) or raw string literals to avoid escape sequence errors.\n",
    "\"\"\"\n",
    "\n",
    "def count_images(directory):\n",
    "    # Create a Path object for the directory\n",
    "    path = Path(directory)\n",
    "    \n",
    "    # Use glob to find all jpg, jpeg, and png files\n",
    "    jpg_files = glob.glob(str(path / '**/*.jpg'), recursive=True)\n",
    "    jpeg_files = glob.glob(str(path / '**/*.jpeg'), recursive=True)\n",
    "    png_files = glob.glob(str(path / '**/*.png'), recursive=True)\n",
    "    \n",
    "    # Count the total number of image files\n",
    "    total_images = len(jpg_files) + len(jpeg_files) + len(png_files)\n",
    "    \n",
    "    return total_images\n",
    "\n",
    "# Specify the directory path\n",
    "directory_path = 'E:\\\\training_dir'\n",
    "\n",
    "# Call the function and print the result\n",
    "print(f'Total number of images: {count_images(directory_path)}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}