k4d3 commited on
Commit
4a5ff1f
1 Parent(s): 6776f94

Signed-off-by: Balazs Horvath <[email protected]>

dataset_tools/done/Convert RGBA to RGB in PNGs.ipynb DELETED
@@ -1,96 +0,0 @@
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/Replace Transparency with Black.ipynb DELETED
@@ -1,110 +0,0 @@
1
- {
2
- "cells": [
3
- {
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
- {
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
- "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",
85
- " process_directory(directory)"
86
- ]
87
- }
88
- ],
89
- "metadata": {
90
- "kernelspec": {
91
- "display_name": "base",
92
- "language": "python",
93
- "name": "python3"
94
- },
95
- "language_info": {
96
- "codemirror_mode": {
97
- "name": "ipython",
98
- "version": 3
99
- },
100
- "file_extension": ".py",
101
- "mimetype": "text/x-python",
102
- "name": "python",
103
- "nbconvert_exporter": "python",
104
- "pygments_lexer": "ipython3",
105
- "version": "3.12.2"
106
- }
107
- },
108
- "nbformat": 4,
109
- "nbformat_minor": 2
110
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dataset_tools/done/convert_rgba_to_rgb_in_pngs.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import glob
3
+ import multiprocessing
4
+ from PIL import Image
5
+
6
+ # Set the maximum number of pixels allowed in an image to prevent DecompressionBombWarning.
7
+ Image.MAX_IMAGE_PIXELS = 139211472
8
+
9
+
10
+ def convert_rgba_to_rgb(image_path):
11
+ """
12
+ Convert an RGBA image to RGB format.
13
+ """
14
+ try:
15
+ print(f"Opening image: {image_path}")
16
+ with Image.open(image_path) as image:
17
+ print(f"Image mode: {image.mode}")
18
+ if image.mode == "RGBA":
19
+ rgb_image = image.convert("RGB")
20
+ rgb_image.save(image_path)
21
+ print(f"Converted {image_path} to RGB.")
22
+ else:
23
+ print(f"{image_path} is not an RGBA image.")
24
+ except Exception as e:
25
+ print(f"Error processing {image_path}: {e}")
26
+
27
+
28
+ def main():
29
+ """
30
+ Main function to convert all RGBA images to RGB in a directory.
31
+ """
32
+ directory = r"E:\training_dir"
33
+ print(f"Directory set to: {directory}")
34
+
35
+ # Get all .png files in the directory recursively
36
+ files = glob.glob(os.path.join(directory, "**", "*.png"), recursive=True)
37
+ print(f"Found {len(files)} .png files in directory and subdirectories.")
38
+
39
+ # Determine the number of processes based on the available CPUs
40
+ num_processes = multiprocessing.cpu_count()
41
+ print(f"Number of processes set to the number of available CPUs: {num_processes}")
42
+
43
+ # Create a pool of processes
44
+ with multiprocessing.Pool(num_processes) as pool:
45
+ print("Pool of processes created.")
46
+ # Map the convert_rgba_to_rgb function to the files
47
+ pool.map(convert_rgba_to_rgb, files)
48
+
49
+ print("Conversion complete.")
50
+
51
+
52
+ if __name__ == "__main__":
53
+ main()
dataset_tools/done/replace_transparency_with_black.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import glob
3
+ from multiprocessing import Pool
4
+ from PIL import Image
5
+
6
+
7
+ def add_black_layer(image_path):
8
+ """
9
+ Adds a black layer to the image at the given path and saves the modified image.
10
+
11
+ This function opens an image, converts it to 'RGBA' mode, creates a new black layer,
12
+ pastes the original image onto the black layer, and saves the result back to the disk.
13
+
14
+ Parameters:
15
+ image_path (str): The file path to the image to be processed.
16
+
17
+ Raises:
18
+ Exception: If there is an error opening or processing the image.
19
+ """
20
+ print(f"Processing {image_path}...")
21
+ try:
22
+ with Image.open(image_path) as img:
23
+ img = img.convert("RGBA")
24
+ black_layer = Image.new("RGBA", img.size, (0, 0, 0, 255))
25
+ black_layer.paste(img, (0, 0), img)
26
+ black_layer.save(image_path)
27
+ print(f"Black layer added to {image_path}")
28
+ except Exception as e:
29
+ print(f"Error processing {image_path}: {e}")
30
+ raise
31
+
32
+
33
+ def process_image(image_path):
34
+ """
35
+ Processes a single image by adding a black layer.
36
+
37
+ This function is designed to be used with multiprocessing. It calls the 'add_black_layer'
38
+ function and handles any exceptions that occur.
39
+
40
+ Parameters:
41
+ image_path (str): The file path to the image to be processed.
42
+ """
43
+ try:
44
+ add_black_layer(image_path)
45
+ print(f"Black layer added to and overwritten {image_path}")
46
+ except Exception as e:
47
+ print(f"Error processing {image_path}: {e}")
48
+
49
+
50
+ def process_directory(directory):
51
+ """
52
+ Processes all .png images in a directory by adding a black layer to each.
53
+
54
+ This function finds all .png images within the specified directory (including subdirectories),
55
+ then creates a pool of worker processes to process each image in parallel.
56
+
57
+ Parameters:
58
+ directory (str): The directory path where the .png images are located.
59
+ """
60
+ image_paths = glob.glob(os.path.join(directory, "**", "*.png"), recursive=True)
61
+ print(f"Found {len(image_paths)} images to process.")
62
+ with Pool() as pool:
63
+ pool.map(add_black_layer, image_paths)
64
+
65
+
66
+ if __name__ == "__main__":
67
+ directory = r"E:\training_dir"
68
+ print(f"Starting processing of images in {directory}")
69
+ process_directory(directory)