Skip to content

Instantly share code, notes, and snippets.

@alexllc
Created January 24, 2024 18:10
Show Gist options
  • Save alexllc/d928eea347a8c6c9faa22bccbe2e7d4f to your computer and use it in GitHub Desktop.
Save alexllc/d928eea347a8c6c9faa22bccbe2e7d4f to your computer and use it in GitHub Desktop.
Bash and python script to use ImageMagick to convert non-pyramidal images to tiff pyramid images and add openslide compatible metadata
"""Python script to spoof metadata to converted tiff files
This script adds necessary metadata to images converted from tif/jpg/png and are not compatible with openslide format. Using the tifftools library, we can spoof the metadata to make it compatible with openslide or other image processing libraries. This script is created using the guide from http://www.andrewjanowczyk.com/converting-an-existing-image-into-an-openslide-compatible-format/
This script requires the following packages:
- argparse
- tifftools
You need to specify the power and microns per pixel for all images, run them separately if you have different specifications for different images.
"""
# import arguments
import os
import glob
import argparse
import tifftools
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_path', help="input file", type = str)
parser.add_argument('-o', '--output_path', help="output file", type = str)
parser.add_argument('-m', '--mpp', help = "microns per pixel", type = str)
parser.add_argument('-p', '--power', help = "power", type = str)
args = parser.parse_args()
# search for files in input path that ends with .tif or .tiff
tif_list = glob.glob(args.input_path + '/*.tif')
tif_list += glob.glob(args.input_path + '/*.tiff')
# for each tiff file in the list, add metadata
for tif in tif_list:
info = tifftools.read_tiff(tif)
metadata_string = 'Aperio Fake |AppMag = {}|MPP = {}'.format(args.power, args.mpp)
info['ifds'][0]['tags'][tifftools.Tag.ImageDescription.value] = {'data': metadata_string, 'datatype': tifftools.Datatype.ASCII}
tif_name = tif.split('/')[-1].split('.')[0]
tifftools.write_tiff(info, os.path.join(args.output_path, tif_name + '_tagged.tif'))
#!/bin/bash
input_directory="/input/image/path"
output_directory="/output/image/path"
# Check if the input directory exists
if [ ! -d "$input_directory" ]; then
echo "Input directory not found."
exit 1
fi
# Check if the output directory exists, create it if not
if [ ! -d "$output_directory" ]; then
mkdir -p "$output_directory"
fi
# Iterate over all files named img_x.jpg in the input directory
for file in "$input_directory"/*; do
if [ -f "$file" ]; then
# Extract the filename without the extension
filename=$(basename -- "$file")
echo "$filename"
filename_no_extension="${filename%.*}"
# Run the ImageMagick command
magick "$file" -define tiff:tile-geometry=256x256 -units PixelsPerCentimeter -compress jpeg ptif:"$output_directory/${filename_no_extension}.tiff"
fi
done
echo "Conversion complete."
echo "Adding metadata..."
python ./add_metadata_to_tif.py -i "$output_directory" -o "$output_directory" -p '20' -m '0.5'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment