{ "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 }