wow
Browse filesSigned-off-by: Balazs Horvath <acsipont@gmail.com>
- dataset_tools/Create Empty txt Files for Images.ipynb +0 -144
- dataset_tools/done/Convert RGBA to RGB in PNGs.ipynb +96 -0
- dataset_tools/done/Count Images in Folder.ipynb +92 -0
- dataset_tools/done/Create Empty Captions for Images.ipynb +95 -0
- dataset_tools/{Newlines to Commas.ipynb → done/Newlines to Commas.ipynb} +37 -16
- dataset_tools/done/Replace Transparency with Black.ipynb +43 -10
dataset_tools/Create Empty txt Files for Images.ipynb
DELETED
@@ -1,144 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": 1,
|
6 |
-
"metadata": {},
|
7 |
-
"outputs": [
|
8 |
-
{
|
9 |
-
"name": "stdout",
|
10 |
-
"output_type": "stream",
|
11 |
-
"text": [
|
12 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\00a58ce07a5186d4edbe9eb29bd3fe53_1.txt\n",
|
13 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\044ce673a55771bd2b5700318547bb89.txt\n",
|
14 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\06b6dc04ce7e684216090f6a333a0f24.txt\n",
|
15 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\0b5371361af9d5706e010e6b91af8269.txt\n",
|
16 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\0b5ead570498882ea2c4f83970007faf.txt\n",
|
17 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\127ab13e2bb2c049e409eb7fdcbc9b7a.txt\n",
|
18 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\1308b781e85b8c64d7ee3ba1b64bf444.txt\n",
|
19 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\1ad66dc78c035d3241601072bb41a455.txt\n",
|
20 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\1caf765f1a0301e47680520dab472744.txt\n",
|
21 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\20aad729e70607539a47d0a2ccdf382b.txt\n",
|
22 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\2241b31138f5ada56d5e14ba371f82e7.txt\n",
|
23 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\22e6a9e62f29ac02ea1fbc78f6bd304f.txt\n",
|
24 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\24ea0ace58127a97cff268843615744f.txt\n",
|
25 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\2877886ddfdd0177f831dccb8e68aa4f.txt\n",
|
26 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\2e15a82de44733126b9982987f5badf1.txt\n",
|
27 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\2f9f20922622a03f1c94669a6f8c00f5.txt\n",
|
28 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\319aa3ba392b11796db84e266b58cd38.txt\n",
|
29 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\330d109c6b45475765563acb5780c44c.txt\n",
|
30 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\331101a6a21b450274b3b3e90d7593c6.txt\n",
|
31 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\33e345444a93860e274794d2dd3702a1.txt\n",
|
32 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\3439faa91c896be8787813572547b2a9.txt\n",
|
33 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\351af7711b3a57c84c0c28dfce9641b3.txt\n",
|
34 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\3cead9d7fccf871396db6f1986ad1188.txt\n",
|
35 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\42681e66733bdfd38800eb01c9b21522.txt\n",
|
36 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\4402451c6dc515356d25e10c620f2003.txt\n",
|
37 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\584a8f97df450c7742ecc1df77ed37fe.txt\n",
|
38 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\5ade980fb60341029c46d927948cffcc.txt\n",
|
39 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\5c6d81b6743953086bb1f6c7d2bbbd70_1.txt\n",
|
40 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\6c6f0b0fd01b8cf509e44bac38032a15.txt\n",
|
41 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\7051f37f3a100ecd09b8b704f7756fe3.txt\n",
|
42 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\744500bacde986871a6d4f464266a915.txt\n",
|
43 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\76fc8b8c80b70271f09c383c42da1d69.txt\n",
|
44 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\77aeebd0874339a181566c9d683ee0c0.txt\n",
|
45 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\79009dc71f54764093fc00fe30bfe1ec.txt\n",
|
46 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\82faee78f1ad3c32ca9045619c04d50e.txt\n",
|
47 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\856a37de0b35e336bc17bad772badc79.txt\n",
|
48 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\8b3e3bc11f014e3a9ad5ab2be648fc2c.txt\n",
|
49 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\90e06213e04da5adedeafbdb9cd365eb.txt\n",
|
50 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\94f4e009078007e3ed1051bf89de4034.txt\n",
|
51 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\9a104277b8f586d9090864bba6ffb4c3.txt\n",
|
52 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\9a1e5a6ebd91cc2263503c44f85b2335.txt\n",
|
53 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\9e995b2e5a51203aadade15076c3e43b.txt\n",
|
54 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\a1bb3892776dbaa41ebb9860f5173956.txt\n",
|
55 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\a3ddc2fbe220dc85b585294e1b7b355f.txt\n",
|
56 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\a493be0b29b9390f046795cbae93b0d5.txt\n",
|
57 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\a4fffd4a03a0bef737b7cfec5a8cc6f4.txt\n",
|
58 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\adbda3e546986a40f2476ef8f3e07118.txt\n",
|
59 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\afd1c3f2f8ea7693fb7a6cb692b50403.txt\n",
|
60 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\b5ca2054eb91296ba19809ae1d306b4d.txt\n",
|
61 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\b8f87e4deca6cbf37396e66447e31921.txt\n",
|
62 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\ba77c681986211c9b7da20b5050859a3.txt\n",
|
63 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\babaf50487ea84064971eb0d1757df23.txt\n",
|
64 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\bafc1dc4937f7ecf7cc707d32500b60d.txt\n",
|
65 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\c2b56828f5b306a9d1ef2f4970cb2a46.txt\n",
|
66 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\c8f28965c1bde921f89e7f7e49c0b078.txt\n",
|
67 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\d125439867ab61520cf8a5f9391a0cd9.txt\n",
|
68 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\d28945f8c9556d071028356457f16c34.txt\n",
|
69 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\d309a6abca98c989f24f3050c2a1bdd1.txt\n",
|
70 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\d709a20dff4ec44d84a446caac98d276.txt\n",
|
71 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\d756957fe6549e07e9b1236d36561bde.txt\n",
|
72 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\e1515ab206479a8673f14e1ae6fa66b0.txt\n",
|
73 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\eccdc7a822051408cab1f0e791e4e308.txt\n",
|
74 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\ecd6c90a0ea837644ca91faab0af71e0.txt\n",
|
75 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\f3062506cd9a84097f937f46181bfc4f.txt\n",
|
76 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\f82a37b693c9c6b626f1fe9b16840d3e.txt\n",
|
77 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\f873d55150075322fd60a47bdf940420.txt\n",
|
78 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\faaea9eccdc0e63abe7379fdf21d6fee.txt\n",
|
79 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\fdffb5c652d76e5ff33ab65d365b6802.txt\n",
|
80 |
-
"Created C:\\Users\\kade\\Desktop\\1_by_spaceengine\\fe5f1e6bd6a302b4830e34823a54bc34.txt\n"
|
81 |
-
]
|
82 |
-
}
|
83 |
-
],
|
84 |
-
"source": [
|
85 |
-
"import os\n",
|
86 |
-
"\n",
|
87 |
-
"def create_empty_txt_files(directory):\n",
|
88 |
-
" # Check if the directory exists\n",
|
89 |
-
" if not os.path.exists(directory):\n",
|
90 |
-
" print(\"Directory does not exist.\")\n",
|
91 |
-
" return\n",
|
92 |
-
" \n",
|
93 |
-
" # Get a list of all files in the directory\n",
|
94 |
-
" files = os.listdir(directory)\n",
|
95 |
-
" \n",
|
96 |
-
" # Iterate over each file\n",
|
97 |
-
" for file in files:\n",
|
98 |
-
" # Check if the file is an image (you can add more image extensions if needed)\n",
|
99 |
-
" if file.endswith(\".jpg\") or file.endswith(\".png\") or file.endswith(\".jpeg\"):\n",
|
100 |
-
" # Extract the filename without extension\n",
|
101 |
-
" filename = os.path.splitext(file)[0]\n",
|
102 |
-
" \n",
|
103 |
-
" # Create a corresponding txt file with the same name\n",
|
104 |
-
" txt_filename = os.path.join(directory, filename + \".txt\")\n",
|
105 |
-
" \n",
|
106 |
-
" # Check if the txt file already exists\n",
|
107 |
-
" if not os.path.exists(txt_filename):\n",
|
108 |
-
" # Create an empty txt file\n",
|
109 |
-
" with open(txt_filename, 'w') as f:\n",
|
110 |
-
" pass\n",
|
111 |
-
" print(f\"Created {txt_filename}\")\n",
|
112 |
-
" else:\n",
|
113 |
-
" print(f\"{txt_filename} already exists\")\n",
|
114 |
-
"\n",
|
115 |
-
"# Path to the directory containing the images\n",
|
116 |
-
"image_directory = r'C:\\Users\\kade\\Desktop\\1_by_spaceengine'\n",
|
117 |
-
"\n",
|
118 |
-
"# Call the function to create empty txt files\n",
|
119 |
-
"create_empty_txt_files(image_directory)"
|
120 |
-
]
|
121 |
-
}
|
122 |
-
],
|
123 |
-
"metadata": {
|
124 |
-
"kernelspec": {
|
125 |
-
"display_name": "Python 3",
|
126 |
-
"language": "python",
|
127 |
-
"name": "python3"
|
128 |
-
},
|
129 |
-
"language_info": {
|
130 |
-
"codemirror_mode": {
|
131 |
-
"name": "ipython",
|
132 |
-
"version": 3
|
133 |
-
},
|
134 |
-
"file_extension": ".py",
|
135 |
-
"mimetype": "text/x-python",
|
136 |
-
"name": "python",
|
137 |
-
"nbconvert_exporter": "python",
|
138 |
-
"pygments_lexer": "ipython3",
|
139 |
-
"version": "3.12.3"
|
140 |
-
}
|
141 |
-
},
|
142 |
-
"nbformat": 4,
|
143 |
-
"nbformat_minor": 2
|
144 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataset_tools/done/Convert RGBA to RGB in PNGs.ipynb
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"# Convert RGBA to RGB in PNGs\n",
|
8 |
+
"\n",
|
9 |
+
"---\n",
|
10 |
+
"\n",
|
11 |
+
"This Python script automates the process of converting `.png` images from RGBA to RGB format in a specified directory, utilizing multiprocessing to enhance efficiency."
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": null,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"import os\n",
|
21 |
+
"from PIL import Image\n",
|
22 |
+
"import glob\n",
|
23 |
+
"import multiprocessing\n",
|
24 |
+
"\n",
|
25 |
+
"# Set the maximum number of pixels allowed in an image to prevent DecompressionBombWarning.\n",
|
26 |
+
"Image.MAX_IMAGE_PIXELS = 139211472\n",
|
27 |
+
"\n",
|
28 |
+
"def convert_rgba_to_rgb(image_path):\n",
|
29 |
+
" \"\"\"\n",
|
30 |
+
" Convert an RGBA image to RGB format.\n",
|
31 |
+
"\n",
|
32 |
+
" This function opens an image from a given path and checks if it's in RGBA mode.\n",
|
33 |
+
" If it is, the image is converted to RGB mode and saved back to the same path.\n",
|
34 |
+
" Any errors encountered during processing are caught and printed.\n",
|
35 |
+
"\n",
|
36 |
+
" Parameters:\n",
|
37 |
+
" - image_path (str): The file path of the image to be converted.\n",
|
38 |
+
"\n",
|
39 |
+
" Returns:\n",
|
40 |
+
" None\n",
|
41 |
+
" \"\"\"\n",
|
42 |
+
" try:\n",
|
43 |
+
" with Image.open(image_path) as image:\n",
|
44 |
+
" if image.mode == 'RGBA':\n",
|
45 |
+
" rgb_image = image.convert('RGB')\n",
|
46 |
+
" rgb_image.save(image_path)\n",
|
47 |
+
" print(f\"Converted {image_path} to RGB.\")\n",
|
48 |
+
" else:\n",
|
49 |
+
" print(f\"{image_path} is not an RGBA image.\")\n",
|
50 |
+
" except Exception as e:\n",
|
51 |
+
" print(f\"Error processing {image_path}: {e}\")\n",
|
52 |
+
"\n",
|
53 |
+
"def main():\n",
|
54 |
+
" \"\"\"\n",
|
55 |
+
" Main function to convert all RGBA images to RGB in a directory.\n",
|
56 |
+
"\n",
|
57 |
+
" This function searches for all .png files in a specified directory and its subdirectories.\n",
|
58 |
+
" It then creates a pool of processes equal to the number of available CPUs and uses them\n",
|
59 |
+
" to convert each RGBA image to RGB format concurrently.\n",
|
60 |
+
"\n",
|
61 |
+
" Returns:\n",
|
62 |
+
" None\n",
|
63 |
+
" \"\"\"\n",
|
64 |
+
" directory = r'E:\\training_dir'\n",
|
65 |
+
" # Get all .png files in the directory recursively\n",
|
66 |
+
" files = glob.glob(os.path.join(directory, '**', '*.png'), recursive=True)\n",
|
67 |
+
" \n",
|
68 |
+
" # Determine the number of processes based on the available CPUs\n",
|
69 |
+
" num_processes = multiprocessing.cpu_count()\n",
|
70 |
+
"\n",
|
71 |
+
" # Create a pool of processes\n",
|
72 |
+
" with multiprocessing.Pool(num_processes) as pool:\n",
|
73 |
+
" # Map the convert_rgba_to_rgb function to the files\n",
|
74 |
+
" pool.map(convert_rgba_to_rgb, files)\n",
|
75 |
+
"\n",
|
76 |
+
" print(\"Conversion complete.\")\n",
|
77 |
+
"\n",
|
78 |
+
"if __name__ == \"__main__\":\n",
|
79 |
+
" main()"
|
80 |
+
]
|
81 |
+
}
|
82 |
+
],
|
83 |
+
"metadata": {
|
84 |
+
"kernelspec": {
|
85 |
+
"display_name": "base",
|
86 |
+
"language": "python",
|
87 |
+
"name": "python3"
|
88 |
+
},
|
89 |
+
"language_info": {
|
90 |
+
"name": "python",
|
91 |
+
"version": "3.12.2"
|
92 |
+
}
|
93 |
+
},
|
94 |
+
"nbformat": 4,
|
95 |
+
"nbformat_minor": 2
|
96 |
+
}
|
dataset_tools/done/Count Images in Folder.ipynb
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"# Count Images in Folder\n",
|
8 |
+
"\n",
|
9 |
+
"---\n",
|
10 |
+
"\n",
|
11 |
+
"This script counts the total number of JPEG and PNG images in a specified directory."
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 1,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [
|
19 |
+
{
|
20 |
+
"name": "stdout",
|
21 |
+
"output_type": "stream",
|
22 |
+
"text": [
|
23 |
+
"Total number of images: 21891\n"
|
24 |
+
]
|
25 |
+
}
|
26 |
+
],
|
27 |
+
"source": [
|
28 |
+
"from pathlib import Path\n",
|
29 |
+
"import glob\n",
|
30 |
+
"\n",
|
31 |
+
"\"\"\"\n",
|
32 |
+
"This script counts the total number of JPEG and PNG images in a specified directory.\n",
|
33 |
+
"\n",
|
34 |
+
"The script uses the `glob` module to find all files with .jpg, .jpeg, and .png extensions\n",
|
35 |
+
"within the given directory and its subdirectories. It then calculates the total count of these\n",
|
36 |
+
"image files and prints the result.\n",
|
37 |
+
"\n",
|
38 |
+
"Functions:\n",
|
39 |
+
" count_images(directory): Counts and returns the number of image files in the directory.\n",
|
40 |
+
"\n",
|
41 |
+
"Usage:\n",
|
42 |
+
" To use this script, simply set the `directory_path` variable to the path of the directory\n",
|
43 |
+
" you want to process. Then run the script, and it will output the total number of images found.\n",
|
44 |
+
"\n",
|
45 |
+
"Note:\n",
|
46 |
+
" Ensure that the directory path uses double backslashes (\\\\) or raw string literals to avoid escape sequence errors.\n",
|
47 |
+
"\"\"\"\n",
|
48 |
+
"\n",
|
49 |
+
"def count_images(directory):\n",
|
50 |
+
" # Create a Path object for the directory\n",
|
51 |
+
" path = Path(directory)\n",
|
52 |
+
" \n",
|
53 |
+
" # Use glob to find all jpg, jpeg, and png files\n",
|
54 |
+
" jpg_files = glob.glob(str(path / '**/*.jpg'), recursive=True)\n",
|
55 |
+
" jpeg_files = glob.glob(str(path / '**/*.jpeg'), recursive=True)\n",
|
56 |
+
" png_files = glob.glob(str(path / '**/*.png'), recursive=True)\n",
|
57 |
+
" \n",
|
58 |
+
" # Count the total number of image files\n",
|
59 |
+
" total_images = len(jpg_files) + len(jpeg_files) + len(png_files)\n",
|
60 |
+
" \n",
|
61 |
+
" return total_images\n",
|
62 |
+
"\n",
|
63 |
+
"# Specify the directory path\n",
|
64 |
+
"directory_path = 'E:\\\\training_dir'\n",
|
65 |
+
"\n",
|
66 |
+
"# Call the function and print the result\n",
|
67 |
+
"print(f'Total number of images: {count_images(directory_path)}')"
|
68 |
+
]
|
69 |
+
}
|
70 |
+
],
|
71 |
+
"metadata": {
|
72 |
+
"kernelspec": {
|
73 |
+
"display_name": "base",
|
74 |
+
"language": "python",
|
75 |
+
"name": "python3"
|
76 |
+
},
|
77 |
+
"language_info": {
|
78 |
+
"codemirror_mode": {
|
79 |
+
"name": "ipython",
|
80 |
+
"version": 3
|
81 |
+
},
|
82 |
+
"file_extension": ".py",
|
83 |
+
"mimetype": "text/x-python",
|
84 |
+
"name": "python",
|
85 |
+
"nbconvert_exporter": "python",
|
86 |
+
"pygments_lexer": "ipython3",
|
87 |
+
"version": "3.12.2"
|
88 |
+
}
|
89 |
+
},
|
90 |
+
"nbformat": 4,
|
91 |
+
"nbformat_minor": 2
|
92 |
+
}
|
dataset_tools/done/Create Empty Captions for Images.ipynb
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"# Create Empty Captions for Images\n",
|
8 |
+
"\n",
|
9 |
+
"---\n",
|
10 |
+
"\n",
|
11 |
+
"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."
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": null,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"import os\n",
|
21 |
+
"import glob\n",
|
22 |
+
"\n",
|
23 |
+
"def create_empty_txt_files(directory):\n",
|
24 |
+
" \"\"\"\n",
|
25 |
+
" Create empty .txt files for each image in a specified directory.\n",
|
26 |
+
"\n",
|
27 |
+
" This function checks for the existence of a directory and then iterates over all .jpg, .png, and .jpeg files within it.\n",
|
28 |
+
" For each image file, it creates an empty .txt file with the same name if it doesn't already exist.\n",
|
29 |
+
"\n",
|
30 |
+
" Parameters:\n",
|
31 |
+
" - directory (str): The path to the directory where the image files are located and where the .txt files will be created.\n",
|
32 |
+
"\n",
|
33 |
+
" Returns:\n",
|
34 |
+
" None: This function does not return any value.\n",
|
35 |
+
"\n",
|
36 |
+
" Prints:\n",
|
37 |
+
" - A message indicating that a .txt file has been created or already exists for each image file.\n",
|
38 |
+
" \"\"\"\n",
|
39 |
+
" # Check if the directory exists\n",
|
40 |
+
" if not os.path.exists(directory):\n",
|
41 |
+
" print(\"Directory does not exist.\")\n",
|
42 |
+
" return\n",
|
43 |
+
"\n",
|
44 |
+
" # Get a list of all image files in the directory\n",
|
45 |
+
" image_files = glob.glob(os.path.join(directory, \"*.jpg\")) + \\\n",
|
46 |
+
" glob.glob(os.path.join(directory, \"*.png\")) + \\\n",
|
47 |
+
" glob.glob(os.path.join(directory, \"*.jpeg\"))\n",
|
48 |
+
"\n",
|
49 |
+
" # Iterate over each image file\n",
|
50 |
+
" for image_file in image_files:\n",
|
51 |
+
" # Extract the filename without extension\n",
|
52 |
+
" filename = os.path.splitext(os.path.basename(image_file))[0]\n",
|
53 |
+
"\n",
|
54 |
+
" # Create a corresponding txt file with the same name\n",
|
55 |
+
" txt_filename = os.path.join(directory, filename + \".txt\")\n",
|
56 |
+
"\n",
|
57 |
+
" # Check if the txt file already exists\n",
|
58 |
+
" if not os.path.exists(txt_filename):\n",
|
59 |
+
" # Create an empty txt file\n",
|
60 |
+
" with open(txt_filename, 'w') as f:\n",
|
61 |
+
" pass\n",
|
62 |
+
" print(f\"Created {txt_filename}\")\n",
|
63 |
+
" else:\n",
|
64 |
+
" print(f\"{txt_filename} already exists\")\n",
|
65 |
+
"\n",
|
66 |
+
"# Path to the directory containing the images\n",
|
67 |
+
"image_directory = r'C:\\Users\\kade\\Desktop\\training_dir_staging\\1_by_spaceengine'\n",
|
68 |
+
"\n",
|
69 |
+
"# Call the function to create empty txt files\n",
|
70 |
+
"create_empty_txt_files(image_directory)"
|
71 |
+
]
|
72 |
+
}
|
73 |
+
],
|
74 |
+
"metadata": {
|
75 |
+
"kernelspec": {
|
76 |
+
"display_name": "Python 3",
|
77 |
+
"language": "python",
|
78 |
+
"name": "python3"
|
79 |
+
},
|
80 |
+
"language_info": {
|
81 |
+
"codemirror_mode": {
|
82 |
+
"name": "ipython",
|
83 |
+
"version": 3
|
84 |
+
},
|
85 |
+
"file_extension": ".py",
|
86 |
+
"mimetype": "text/x-python",
|
87 |
+
"name": "python",
|
88 |
+
"nbconvert_exporter": "python",
|
89 |
+
"pygments_lexer": "ipython3",
|
90 |
+
"version": "3.12.3"
|
91 |
+
}
|
92 |
+
},
|
93 |
+
"nbformat": 4,
|
94 |
+
"nbformat_minor": 2
|
95 |
+
}
|
dataset_tools/{Newlines to Commas.ipynb → done/Newlines to Commas.ipynb}
RENAMED
@@ -16,24 +16,45 @@
|
|
16 |
"metadata": {},
|
17 |
"outputs": [],
|
18 |
"source": [
|
19 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
"\n",
|
21 |
"def process_directory(directory):\n",
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
29 |
-
"
|
30 |
-
"
|
31 |
-
"
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
"\n",
|
38 |
"# Directory path\n",
|
39 |
"directory_path = r'C:\\Users\\kade\\Desktop\\training_dir_staging'\n",
|
|
|
16 |
"metadata": {},
|
17 |
"outputs": [],
|
18 |
"source": [
|
19 |
+
"\"\"\"\n",
|
20 |
+
"This script is designed to process all text files within a specified directory\n",
|
21 |
+
"and its subdirectories. It modifies the content of each text file by replacing\n",
|
22 |
+
"newlines with a comma followed by a space.\n",
|
23 |
+
"\n",
|
24 |
+
"Functions:\n",
|
25 |
+
" process_directory(directory): Recursively processes all '.txt' files in the given directory.\n",
|
26 |
+
"\n",
|
27 |
+
"Args:\n",
|
28 |
+
" directory (str): The path to the directory containing the text files to be processed.\n",
|
29 |
+
"\n",
|
30 |
+
"Usage:\n",
|
31 |
+
" Set the 'directory_path' variable to the path of the target directory and run the script.\n",
|
32 |
+
" The script will modify all '.txt' files within this directory and its subdirectories.\n",
|
33 |
+
"\"\"\"\n",
|
34 |
+
"from pathlib import Path\n",
|
35 |
"\n",
|
36 |
"def process_directory(directory):\n",
|
37 |
+
" \"\"\"\n",
|
38 |
+
" Process all '.txt' files in the given directory, replacing newlines with a comma and space.\n",
|
39 |
+
"\n",
|
40 |
+
" Parameters:\n",
|
41 |
+
" - directory (str): The path to the directory to process.\n",
|
42 |
+
" \"\"\"\n",
|
43 |
+
" # Create a Path object for the directory\n",
|
44 |
+
" path = Path(directory)\n",
|
45 |
+
" \n",
|
46 |
+
" # Use glob pattern to match all .txt files recursively\n",
|
47 |
+
" for file_path in path.rglob('*.txt'):\n",
|
48 |
+
" # Read the content of the file\n",
|
49 |
+
" with open(file_path, 'r') as file:\n",
|
50 |
+
" content = file.read()\n",
|
51 |
+
" \n",
|
52 |
+
" # Replace newline with a comma and space\n",
|
53 |
+
" modified_content = content.replace('\\n', ', ')\n",
|
54 |
+
" \n",
|
55 |
+
" # Write the modified content back to the file\n",
|
56 |
+
" with open(file_path, 'w') as file:\n",
|
57 |
+
" file.write(modified_content)\n",
|
58 |
"\n",
|
59 |
"# Directory path\n",
|
60 |
"directory_path = r'C:\\Users\\kade\\Desktop\\training_dir_staging'\n",
|
dataset_tools/done/Replace Transparency with Black.ipynb
CHANGED
@@ -4,10 +4,11 @@
|
|
4 |
"cell_type": "markdown",
|
5 |
"metadata": {},
|
6 |
"source": [
|
7 |
-
"
|
|
|
8 |
"----\n",
|
9 |
"\n",
|
10 |
-
"This Python script
|
11 |
]
|
12 |
},
|
13 |
{
|
@@ -19,33 +20,65 @@
|
|
19 |
"import os\n",
|
20 |
"from PIL import Image\n",
|
21 |
"import glob\n",
|
|
|
22 |
"\n",
|
23 |
"def add_black_layer(image_path):\n",
|
24 |
" \"\"\"\n",
|
25 |
-
" Adds a black layer to the image at the given path and
|
|
|
|
|
|
|
26 |
"\n",
|
27 |
" Parameters:\n",
|
28 |
-
" image_path (str): The file path to the image.\n",
|
|
|
|
|
|
|
29 |
" \"\"\"\n",
|
30 |
" try:\n",
|
31 |
" with Image.open(image_path) as img:\n",
|
32 |
-
"
|
|
|
|
|
33 |
" black_layer.paste(img, (0, 0), img)\n",
|
34 |
" black_layer.save(image_path)\n",
|
35 |
" print(f\"Black layer added to and overwritten {image_path}\")\n",
|
36 |
" except Exception as e:\n",
|
37 |
" print(f\"Error processing {image_path}: {e}\")\n",
|
38 |
"\n",
|
39 |
-
"def
|
40 |
" \"\"\"\n",
|
41 |
-
" Processes
|
|
|
|
|
|
|
42 |
"\n",
|
43 |
" Parameters:\n",
|
44 |
-
"
|
45 |
" \"\"\"\n",
|
46 |
-
"
|
47 |
-
" for image_path in glob.glob(os.path.join(directory, '**', '*.png'), recursive=True):\n",
|
48 |
" add_black_layer(image_path)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
"\n",
|
50 |
"if __name__ == \"__main__\":\n",
|
51 |
" directory = r'E:\\training_dir'\n",
|
|
|
4 |
"cell_type": "markdown",
|
5 |
"metadata": {},
|
6 |
"source": [
|
7 |
+
"# Replace Transparency with Black\n",
|
8 |
+
"\n",
|
9 |
"----\n",
|
10 |
"\n",
|
11 |
+
"This Python script processes all `.png` images in a specified directory by adding a black layer to each, utilizing multiprocessing to handle the images in parallel for efficiency."
|
12 |
]
|
13 |
},
|
14 |
{
|
|
|
20 |
"import os\n",
|
21 |
"from PIL import Image\n",
|
22 |
"import glob\n",
|
23 |
+
"from multiprocessing import Pool\n",
|
24 |
"\n",
|
25 |
"def add_black_layer(image_path):\n",
|
26 |
" \"\"\"\n",
|
27 |
+
" Adds a black layer to the image at the given path and saves the modified image.\n",
|
28 |
+
"\n",
|
29 |
+
" This function opens an image, converts it to 'RGBA' mode, creates a new black layer,\n",
|
30 |
+
" pastes the original image onto the black layer, and saves the result back to the disk.\n",
|
31 |
"\n",
|
32 |
" Parameters:\n",
|
33 |
+
" image_path (str): The file path to the image to be processed.\n",
|
34 |
+
"\n",
|
35 |
+
" Raises:\n",
|
36 |
+
" Exception: If there is an error opening or processing the image.\n",
|
37 |
" \"\"\"\n",
|
38 |
" try:\n",
|
39 |
" with Image.open(image_path) as img:\n",
|
40 |
+
" # Ensure the image is in 'RGBA' mode to handle transparency\n",
|
41 |
+
" img = img.convert('RGBA')\n",
|
42 |
+
" black_layer = Image.new('RGBA', img.size, (0, 0, 0, 255)) # The fourth value is the alpha channel\n",
|
43 |
" black_layer.paste(img, (0, 0), img)\n",
|
44 |
" black_layer.save(image_path)\n",
|
45 |
" print(f\"Black layer added to and overwritten {image_path}\")\n",
|
46 |
" except Exception as e:\n",
|
47 |
" print(f\"Error processing {image_path}: {e}\")\n",
|
48 |
"\n",
|
49 |
+
"def process_image(image_path):\n",
|
50 |
" \"\"\"\n",
|
51 |
+
" Processes a single image by adding a black layer.\n",
|
52 |
+
"\n",
|
53 |
+
" This function is designed to be used with multiprocessing. It calls the 'add_black_layer'\n",
|
54 |
+
" function and handles any exceptions that occur.\n",
|
55 |
"\n",
|
56 |
" Parameters:\n",
|
57 |
+
" image_path (str): The file path to the image to be processed.\n",
|
58 |
" \"\"\"\n",
|
59 |
+
" try:\n",
|
|
|
60 |
" add_black_layer(image_path)\n",
|
61 |
+
" print(f\"Black layer added to and overwritten {image_path}\")\n",
|
62 |
+
" except Exception as e:\n",
|
63 |
+
" print(f\"Error processing {image_path}: {e}\")\n",
|
64 |
+
"\n",
|
65 |
+
"def process_directory(directory):\n",
|
66 |
+
" \"\"\"\n",
|
67 |
+
" Processes all .png images in a directory by adding a black layer to each.\n",
|
68 |
+
"\n",
|
69 |
+
" This function finds all .png images within the specified directory (including subdirectories),\n",
|
70 |
+
" then creates a pool of worker processes to process each image in parallel.\n",
|
71 |
+
"\n",
|
72 |
+
" Parameters:\n",
|
73 |
+
" directory (str): The directory path where the .png images are located.\n",
|
74 |
+
" \"\"\"\n",
|
75 |
+
" # Get a list of all .png images in the directory recursively\n",
|
76 |
+
" image_paths = glob.glob(os.path.join(directory, '**', '*.png'), recursive=True)\n",
|
77 |
+
" \n",
|
78 |
+
" # Create a pool of workers equal to the number of CPU cores\n",
|
79 |
+
" with Pool() as pool:\n",
|
80 |
+
" # Map the process_image function to the list of image paths\n",
|
81 |
+
" pool.map(process_image, image_paths)\n",
|
82 |
"\n",
|
83 |
"if __name__ == \"__main__\":\n",
|
84 |
" directory = r'E:\\training_dir'\n",
|