Skip to content

Instantly share code, notes, and snippets.

@adamltyson
Created August 16, 2024 09:46
Show Gist options
  • Select an option

  • Save adamltyson/e586dbc5da932822c7a8a8ae9385cbd9 to your computer and use it in GitHub Desktop.

Select an option

Save adamltyson/e586dbc5da932822c7a8a8ae9385cbd9 to your computer and use it in GitHub Desktop.
Pad a series of 2D images
import numpy as np
import tifffile as tiff
from pathlib import Path
# Define the input and output directories
input_dir = Path('/home/adam/Desktop/blackcap/blackcap_stitched/2')
output_dir = Path('/home/adam/Desktop/blackcap/blackcap_stitched_padded/2')
# Ensure the output directory exists
output_dir.mkdir(parents=True, exist_ok=True)
# Define the padding size
pad_width = 25
# Process each file in the input directory
for file_path in input_dir.glob('*.tif*'):
# Load the 2D TIFF file
image = tiff.imread(file_path)
if image.ndim == 2: # Ensure it's a 2D image
# Pad the image on the x and y axes (height and width)
padded_image = np.pad(image, pad_width=((pad_width, pad_width),
(pad_width, pad_width)),
mode='constant', constant_values=0)
# Save the padded image to the output directory
output_path = output_dir / file_path.name
tiff.imwrite(output_path, padded_image)
print(f"Processed and saved: {output_path}")
else:
print(f"Skipping non-2D image: {file_path}")
print("Processing complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment