Added compression and decompression scripts.
Browse files- compress.py +57 -0
- decompress.py +40 -0
compress.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import argparse
|
4 |
+
import tarfile
|
5 |
+
import concurrent.futures
|
6 |
+
|
7 |
+
IMAGE_DIR = "images"
|
8 |
+
COMPRESSED_DIR = "compressed"
|
9 |
+
|
10 |
+
def compress_chunk(chunk, chunk_index):
|
11 |
+
with tarfile.open(os.path.join(COMPRESSED_DIR, f"chunk_{chunk_index}.tar.gz"), "w:gz") as tar:
|
12 |
+
for image_path in chunk:
|
13 |
+
tags_path = os.path.splitext(image_path)[0] + ".txt"
|
14 |
+
if not os.path.isfile(tags_path):
|
15 |
+
continue
|
16 |
+
tar.add(image_path, arcname=os.path.basename(image_path))
|
17 |
+
tar.add(tags_path, arcname=os.path.basename(tags_path))
|
18 |
+
|
19 |
+
def parse_args():
|
20 |
+
parser = argparse.ArgumentParser(description="Compress images and group them into chunks.")
|
21 |
+
parser.add_argument("num_images_per_chunk", nargs=argparse.REMAINDER, help="Number of images per chunk, default to 1024")
|
22 |
+
args = parser.parse_args()
|
23 |
+
if not args.num_images_per_chunk:
|
24 |
+
args.num_images_per_chunk = 1024
|
25 |
+
else:
|
26 |
+
if len(args.num_images_per_chunk) > 1:
|
27 |
+
print("Too many arguments passed, you should only pass 1.")
|
28 |
+
sys.exit(1)
|
29 |
+
try:
|
30 |
+
args.num_images_per_chunk = int(args.num_images_per_chunk[0])
|
31 |
+
if args.num_images_per_chunk < 1:
|
32 |
+
raise ValueError("Number of images per chunk needs to be a positive integer!")
|
33 |
+
except ValueError as e:
|
34 |
+
print("Invalid number of images per chunk set:", e)
|
35 |
+
sys.exit(1)
|
36 |
+
return args
|
37 |
+
|
38 |
+
def main():
|
39 |
+
args = parse_args()
|
40 |
+
image_files = [os.path.join(IMAGE_DIR, f) for f in os.listdir(IMAGE_DIR) if not f.endswith(".txt")]
|
41 |
+
image_files.sort()
|
42 |
+
os.makedirs(COMPRESSED_DIR, exist_ok=True)
|
43 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
|
44 |
+
futures = []
|
45 |
+
for i in range(0, len(image_files), args.num_images_per_chunk):
|
46 |
+
chunk = image_files[i:i + args.num_images_per_chunk]
|
47 |
+
chunk_index = i // args.num_images_per_chunk
|
48 |
+
future = executor.submit(compress_chunk, chunk, chunk_index)
|
49 |
+
futures.append(future)
|
50 |
+
concurrent.futures.wait(futures)
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
try:
|
54 |
+
main()
|
55 |
+
except KeyboardInterrupt:
|
56 |
+
print("\nScript interrupted by user, exiting...")
|
57 |
+
sys.exit(1)
|
decompress.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import argparse
|
4 |
+
import tarfile
|
5 |
+
import concurrent.futures
|
6 |
+
|
7 |
+
IMAGE_DIR = "images"
|
8 |
+
COMPRESSED_DIR = "compressed"
|
9 |
+
|
10 |
+
def decompress_chunk(chunk_file, output_dir):
|
11 |
+
with tarfile.open(chunk_file, "r:gz") as tar:
|
12 |
+
tar.extractall(path=output_dir)
|
13 |
+
|
14 |
+
def parse_args():
|
15 |
+
parser = argparse.ArgumentParser(description="Decompress chunked archives.")
|
16 |
+
parser.add_argument("-i", "--input-dir", default=COMPRESSED_DIR, help="Input directory containing compressed chunks")
|
17 |
+
parser.add_argument("-o", "--output-dir", default=IMAGE_DIR, help="Output directory for decompressed files")
|
18 |
+
args = parser.parse_args()
|
19 |
+
return args
|
20 |
+
|
21 |
+
def main():
|
22 |
+
args = parse_args()
|
23 |
+
if not os.path.exists(args.input_dir):
|
24 |
+
print(f"Input directory \"{args.input_dir}\" does not exist.")
|
25 |
+
sys.exit(1)
|
26 |
+
chunk_files = [os.path.join(args.input_dir, f) for f in os.listdir(args.input_dir) if f.endswith(".tar.gz")]
|
27 |
+
os.makedirs(args.output_dir, exist_ok=True)
|
28 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
|
29 |
+
futures = []
|
30 |
+
for chunk_file in chunk_files:
|
31 |
+
future = executor.submit(decompress_chunk, chunk_file, args.output_dir)
|
32 |
+
futures.append(future)
|
33 |
+
concurrent.futures.wait(futures)
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
try:
|
37 |
+
main()
|
38 |
+
except KeyboardInterrupt:
|
39 |
+
print("\nScript interrupted by user, exiting...")
|
40 |
+
sys.exit(1)
|