From f0a4c940af6e6336350b3717bc5fb53a35ee8326 Mon Sep 17 00:00:00 2001 From: "Michael Pilosov, PhD" Date: Wed, 14 Feb 2024 16:05:32 +0000 Subject: [PATCH] bg filling --- scripts/vips_composite.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/vips_composite.py b/scripts/vips_composite.py index 0e856c6..57d7803 100644 --- a/scripts/vips_composite.py +++ b/scripts/vips_composite.py @@ -4,6 +4,13 @@ import random import os import math +import numpy as np + + +def bg_fill(width: int, height: int, channels: int = 3, fill: int = 255): + a = np.ones(shape=(height, width, channels)) * fill + return pyvips.Image.new_from_array(a) + def create_grid_composite( directory, k, spacing_inches, output_file="output.png", dpi=300 @@ -23,7 +30,8 @@ def create_grid_composite( ] # Calculate the size of the composite image - widths, heights = zip(*[image.size for image in images]) + # widths, heights = zip(*[image.size for image in images]) + widths, heights = [1800], [1800] max_width = max(widths) max_height = max(heights) @@ -32,7 +40,8 @@ def create_grid_composite( total_height = k * max_height + (k + 1) * spacing_pixels # Create a blank image for the composite - composite = pyvips.Image.black(total_width, total_height) + # composite = pyvips.Image.black(total_width, total_height, bands=4) + composite = bg_fill(total_width, total_height, channels=1, fill=255) # Place images into the composite for i, image in enumerate(images): @@ -40,15 +49,19 @@ def create_grid_composite( col = i % k x = col * (max_width + spacing_pixels) + spacing_pixels y = row * (max_height + spacing_pixels) + spacing_pixels - composite = composite.insert(image, x, y) + + composite = composite.insert(image.flatten(background=[255, 255, 255]), x, y) # Save the composite image composite.write_to_file(output_file) + x = pyvips.Image.thumbnail(output_file, 1080 * 4) + x.write_to_file("out_sm.png") + print(output_file) if __name__ == "__main__": # Example usage directory = "/teamspace/studios/this_studio/out_sortcolors/umap/" # Change to your directory path - k = 3 # For a 3x3 grid + k = 10 spacing_inches = 0.5 # Half an inch between images create_grid_composite(directory, k, spacing_inches)