diff --git a/all_256_plot.py b/all_256_plot.py new file mode 100644 index 0000000..87ad428 --- /dev/null +++ b/all_256_plot.py @@ -0,0 +1,82 @@ +# import os +# from PIL import Image +# from hilbertcurve.hilbertcurve import HilbertCurve +from tqdm import tqdm +# +# # Number of images +# num_images = 256 +# +# # Image size (assuming all images have the same size) +# image_size = (4096, 4096) +# +# # Determine the order of the Hilbert curve based on the number of images +# order = 1 +# while 2 ** (2 * order) < num_images: +# order += 1 +# +# # Create a Hilbert curve of the appropriate order +# hilbert_curve = HilbertCurve(order, 2) +# +# # Create a blank image to hold the arranged images +# arranged_image = Image.new('L', (image_size[0] * 2 ** order, image_size[1] * 2 ** order)) +# +# # Load and arrange the images +# for i in tqdm(range(num_images)): +# # Load the image +# image_path = os.path.join(os.getcwd(), f"picture/{i}.png") +# image = Image.open(image_path).convert('L') +# +# # Determine the position of the image in the Hilbert curve +# x, y = hilbert_curve.point_from_distance(i) +# +# # Paste the image into the arranged image +# arranged_image.paste(image, (x * image_size[0], y * image_size[1])) +# +# # Display the arranged image +# # arranged_image.show() +# arranged_image.save("ip.jpg") + +import os +from PIL import Image +from hilbertcurve.hilbertcurve import HilbertCurve + +# Number of images +num_images = 256 + +# Image size (assuming all images have the same size) +original_image_size = 8192 # should be times of 16 +image_size = int(original_image_size / 16) + +# Determine the order of the Hilbert curve based on the number of images +order = 1 +while 2 ** (2 * order) < num_images: + order += 1 + +# Create a Hilbert curve of the appropriate order +hilbert_curve = HilbertCurve(order, 2) + +# Create a blank image to hold the arranged images +arranged_image = Image.new('L', (original_image_size, original_image_size)) + +# Load and arrange the images +for i in tqdm(range(num_images)): + # Load the image as grayscale + image_path = os.path.join(os.getcwd(), f"picture/{i}.png") + image = Image.open(image_path).convert('L') + + # Resize the image to the desired size + image = image.resize((image_size, image_size)) + + # Determine the position of the image in the Hilbert curve + x, y = hilbert_curve.point_from_distance(i) + + # Calculate the coordinates of the image in the arranged image + x_coord = x * image_size + y_coord = y * image_size + + # Paste the image into the arranged image + arranged_image.paste(image, (x_coord, y_coord)) + +# Save the arranged image as a PNG file +arranged_image.save(f'ip_{original_image_size}.png') + diff --git a/ip_4096.png b/ip_4096.png new file mode 100644 index 0000000..6167c8e Binary files /dev/null and b/ip_4096.png differ diff --git a/plot.py b/plot.py index 30dcfa0..fb4a86b 100644 --- a/plot.py +++ b/plot.py @@ -119,6 +119,7 @@ def process(index): path = os.path.join("picture", str(index) + ".png") os.makedirs(os.path.dirname(path), exist_ok=True) image.save(path, format="png") + return def build_cache(): @@ -133,6 +134,7 @@ def build_cache(): DATA = {} for i in tqdm(range(4096)): for j in range(4096): + index = xy_to_hilbert(i, j, 12) DATA[index] = (i, j) prebuilt_hilbertCurve_4096["DATA"] = DATA @@ -140,16 +142,14 @@ def build_cache(): json.dump(prebuilt_hilbertCurve_4096, open("prebuilt_hilbertCurve_4096.json", "w+")) return DATA - -print("Start to load prebuild hilbert curve, usually it will take 30 second or more") -time_start = time.time() -try: # try to load cache - prebuilt_hilbertCurve_4096 = json.load(open("prebuilt_hilbertCurve_4096.json", "r"))["DATA"] -except FileNotFoundError: - prebuilt_hilbertCurve_4096 = build_cache() -print(f"Loaded prebuilt hilbert curve, take {round(time.time() - time_start, 2)} seconds") - if __name__ == '__main__': - with Pool(multiprocessing.cpu_count()) as p: + print("Start to load prebuild hilbert curve, usually it will take 30 second or more") + time_start = time.time() + try: # try to load cache + prebuilt_hilbertCurve_4096 = json.load(open("prebuilt_hilbertCurve_4096.json", "r"))["DATA"] + except FileNotFoundError: + prebuilt_hilbertCurve_4096 = build_cache() + print(f"Loaded prebuilt hilbert curve, take {round(time.time() - time_start, 2)} seconds") + with Pool(multiprocessing.cpu_count()//2) as p: p.map(process, os.listdir("ip"))