Created
August 16, 2024 09:46
-
-
Save adamltyson/e586dbc5da932822c7a8a8ae9385cbd9 to your computer and use it in GitHub Desktop.
Pad a series of 2D images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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