File size: 5,757 Bytes
00b2f69
 
 
 
 
 
 
 
 
 
 
 
 
63be7a2
00b2f69
 
 
63be7a2
 
 
 
 
 
 
 
 
00b2f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f61feac
 
 
 
 
 
 
 
 
00b2f69
 
 
39f509b
00b2f69
 
 
 
 
 
63be7a2
00b2f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63be7a2
00b2f69
 
 
 
 
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
97
98
99
100
101
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rename images and captions to MD5\n",
    "\n",
    "This Python script recursively traverses a specified directory, identifies image files with extensions .jpg, .jpeg, or .png, calculates their MD5 hash values, and renames them accordingly. Additionally, it renames accompanying text files (.txt, .caption, .tags) to match the new filename while preserving their original content associations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileExistsError",
     "evalue": "[WinError 183] Cannot create a file when that file already exists: 'E:\\\\training_dir\\\\auroth_the_winter_wyvern\\\\2440ecda95a0ab0ca236d1c6bc09830e.txt' -> 'E:\\\\training_dir\\\\auroth_the_winter_wyvern\\\\237e548ef2de010bcd17e072ddbbbc02.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileExistsError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 43\u001b[0m\n\u001b[0;32m     40\u001b[0m directory \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mE:\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mtraining_dir\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m     42\u001b[0m \u001b[38;5;66;03m# Call the function to rename files\u001b[39;00m\n\u001b[1;32m---> 43\u001b[0m \u001b[43mrename_files\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdirectory\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     45\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFiles renamed successfully!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[1;32mIn[1], line 37\u001b[0m, in \u001b[0;36mrename_files\u001b[1;34m(directory)\u001b[0m\n\u001b[0;32m     35\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(txt_file):\n\u001b[0;32m     36\u001b[0m     new_txt_file \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(root, new_file_name\u001b[38;5;241m.\u001b[39mreplace(file_ext\u001b[38;5;241m.\u001b[39mlower(), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m+\u001b[39m ext)\n\u001b[1;32m---> 37\u001b[0m     \u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrename\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtxt_file\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnew_txt_file\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mFileExistsError\u001b[0m: [WinError 183] Cannot create a file when that file already exists: 'E:\\\\training_dir\\\\auroth_the_winter_wyvern\\\\2440ecda95a0ab0ca236d1c6bc09830e.txt' -> 'E:\\\\training_dir\\\\auroth_the_winter_wyvern\\\\237e548ef2de010bcd17e072ddbbbc02.txt'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import hashlib\n",
    "\n",
    "def md5(file_path):\n",
    "    \"\"\"Calculate MD5 hash of a file.\"\"\"\n",
    "    hash_md5 = hashlib.md5()\n",
    "    with open(file_path, \"rb\") as f:\n",
    "        for chunk in iter(lambda: f.read(4096), b\"\"):\n",
    "            hash_md5.update(chunk)\n",
    "    return hash_md5.hexdigest()\n",
    "\n",
    "def rename_files(directory):\n",
    "    \"\"\"Recursively rename image and accompanying text files.\"\"\"\n",
    "    for root, _, files in os.walk(directory):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            file_name, file_ext = os.path.splitext(file)\n",
    "            if file_ext.lower() in ('.jpg', '.jpeg', '.png'):\n",
    "                # Calculate MD5 hash\n",
    "                new_file_name = md5(file_path) + file_ext.lower()\n",
    "                # Check if the new filename already exists\n",
    "                if os.path.exists(os.path.join(root, new_file_name)):\n",
    "                    # Add a suffix to make the filename unique\n",
    "                    suffix = 1\n",
    "                    while True:\n",
    "                        new_file_name = md5(file_path) + '_' + str(suffix) + file_ext.lower()\n",
    "                        if not os.path.exists(os.path.join(root, new_file_name)):\n",
    "                            break\n",
    "                        suffix += 1\n",
    "                # Rename image file\n",
    "                os.rename(file_path, os.path.join(root, new_file_name))\n",
    "                # Rename accompanying text files\n",
    "                for ext in ('.txt', '.caption', '.tags', '.pony', '.seaart'):\n",
    "                    txt_file = os.path.join(root, file_name + ext)\n",
    "                    if os.path.exists(txt_file):\n",
    "                        new_txt_file = os.path.join(root, new_file_name.replace(file_ext.lower(), '') + ext)\n",
    "                        os.rename(txt_file, new_txt_file)\n",
    "\n",
    "# Specify the directory\n",
    "directory = r'E:\\training_dir'\n",
    "\n",
    "# Call the function to rename files\n",
    "rename_files(directory)\n",
    "\n",
    "print(\"Files renamed successfully!\")"
   ]
  }
 ],
 "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
}