{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pip install pillow datasets pandas pypng uuid\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Preproccessing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import uuid\n", "import shutil\n", "\n", "def rename_and_move_images(source_dir, target_dir):\n", " # Create the target directory if it doesn't exist\n", " os.makedirs(target_dir, exist_ok=True)\n", "\n", " # List of common image file extensions\n", " image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff')\n", "\n", " # Walk through the source directory and its subdirectories\n", " for root, dirs, files in os.walk(source_dir):\n", " for file in files:\n", " # Check if the file has an image extension\n", " if file.lower().endswith(image_extensions):\n", " # Generate a new filename with UUID\n", " new_filename = str(uuid.uuid4()) + os.path.splitext(file)[1]\n", " \n", " # Construct full file paths\n", " old_path = os.path.join(root, file)\n", " new_path = os.path.join(target_dir, new_filename)\n", " \n", " # Move and rename the file\n", " shutil.move(old_path, new_path)\n", " print(f\"Moved and renamed: {old_path} -> {new_path}\")\n", "\n", "# Usage\n", "source_directory = \"images\"\n", "target_directory = \"train\"\n", "\n", "rename_and_move_images(source_directory, target_directory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extract the Metadata" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "import png\n", "import pandas as pd\n", "\n", "# Directory containing images\n", "image_dir = 'train'\n", "metadata_list = []\n", "\n", "# Function to extract the JSON data from the tEXt chunk in PNG images\n", "def extract_metadata_from_png(image_path):\n", " with open(image_path, 'rb') as f:\n", " reader = png.Reader(file=f)\n", " chunks = reader.chunks()\n", " for chunk_type, chunk_data in chunks:\n", " if chunk_type == b'tEXt':\n", " # Convert bytes to string\n", " chunk_text = chunk_data.decode('latin1')\n", " if 'prompt' in chunk_text:\n", " try:\n", " # Extract JSON string after \"prompt\\0\"\n", " json_str = chunk_text.split('prompt\\0', 1)[1]\n", " json_data = json.loads(json_str)\n", " inputs = json_data.get('3', {}).get('inputs', {})\n", " seed = inputs.get('seed', 'N/A')\n", " positive_prompt = json_data.get('6', {}).get('inputs', {}).get('text', 'N/A')\n", " negative_prompt = json_data.get('7', {}).get('inputs', {}).get('text', 'N/A')\n", " model = json_data.get('4', {}).get('inputs', {}).get('ckpt_name', 'N/A')\n", " steps = inputs.get('steps', 'N/A')\n", " cfg = inputs.get('cfg', 'N/A')\n", " sampler_name = inputs.get('sampler_name', 'N/A')\n", " scheduler = inputs.get('scheduler', 'N/A')\n", " denoise = inputs.get('denoise', 'N/A')\n", " return {\n", " 'seed': seed,\n", " 'positive_prompt': positive_prompt,\n", " 'negative_prompt': negative_prompt,\n", " 'model': model,\n", " 'steps': steps,\n", " 'cfg': cfg,\n", " 'sampler_name': sampler_name,\n", " 'scheduler': scheduler,\n", " 'denoise': denoise\n", " }\n", " except json.JSONDecodeError:\n", " pass\n", " return {}\n", "\n", "# Loop through all images in the directory\n", "for file_name in os.listdir(image_dir):\n", " if file_name.endswith('.png'):\n", " image_path = os.path.join(image_dir, file_name)\n", " metadata = extract_metadata_from_png(image_path)\n", " metadata['file_name'] = file_name\n", " metadata_list.append(metadata)\n", "\n", "# Convert metadata to DataFrame\n", "metadata_df = pd.DataFrame(metadata_list)\n", "\n", "# Ensure 'file_name' is the first column\n", "columns_order = ['file_name', 'seed', 'positive_prompt', 'negative_prompt', 'model', 'steps', 'cfg', 'sampler_name', 'scheduler', 'denoise']\n", "metadata_df = metadata_df[columns_order]\n", "\n", "# Save metadata to a CSV file\n", "metadata_csv_path = 'train/metadata.csv'\n", "metadata_df.to_csv(metadata_csv_path, index=False)\n", "\n", "print(\"Metadata extraction complete. Metadata saved to:\", metadata_csv_path)\n", "\n", "\n" ] } ], "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.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }