File size: 3,545 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
94
95
96
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Empty Captions for Images\n",
    "\n",
    "---\n",
    "\n",
    "This Python script creates an empty text file with the same name as each image file (.jpg, .png, or .jpeg) present in a specified directory. The script checks if the directory exists, and then iterates through all the image files in the directory. For each image file, it creates a corresponding text file with the same name but with a .txt extension in the same directory, unless a text file with that name already exists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "\n",
    "def create_empty_txt_files(directory):\n",
    "    \"\"\"\n",
    "    Create empty .txt files for each image in a specified directory.\n",
    "\n",
    "    This function checks for the existence of a directory and then iterates over all .jpg, .png, and .jpeg files within it.\n",
    "    For each image file, it creates an empty .txt file with the same name if it doesn't already exist.\n",
    "\n",
    "    Parameters:\n",
    "    - directory (str): The path to the directory where the image files are located and where the .txt files will be created.\n",
    "\n",
    "    Returns:\n",
    "    None: This function does not return any value.\n",
    "\n",
    "    Prints:\n",
    "    - A message indicating that a .txt file has been created or already exists for each image file.\n",
    "    \"\"\"\n",
    "    # Check if the directory exists\n",
    "    if not os.path.exists(directory):\n",
    "        print(\"Directory does not exist.\")\n",
    "        return\n",
    "\n",
    "    # Get a list of all image files in the directory\n",
    "    image_files = glob.glob(os.path.join(directory, \"*.jpg\")) + \\\n",
    "                  glob.glob(os.path.join(directory, \"*.png\")) + \\\n",
    "                  glob.glob(os.path.join(directory, \"*.jpeg\"))\n",
    "\n",
    "    # Iterate over each image file\n",
    "    for image_file in image_files:\n",
    "        # Extract the filename without extension\n",
    "        filename = os.path.splitext(os.path.basename(image_file))[0]\n",
    "\n",
    "        # Create a corresponding txt file with the same name\n",
    "        txt_filename = os.path.join(directory, filename + \".txt\")\n",
    "\n",
    "        # Check if the txt file already exists\n",
    "        if not os.path.exists(txt_filename):\n",
    "            # Create an empty txt file\n",
    "            with open(txt_filename, 'w') as f:\n",
    "                pass\n",
    "            print(f\"Created {txt_filename}\")\n",
    "        else:\n",
    "            print(f\"{txt_filename} already exists\")\n",
    "\n",
    "# Path to the directory containing the images\n",
    "image_directory = r'C:\\Users\\kade\\Desktop\\training_dir_staging\\1_by_spaceengine'\n",
    "\n",
    "# Call the function to create empty txt files\n",
    "create_empty_txt_files(image_directory)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}