Commit 3b3af42d authored by Adrian Nievergelt's avatar Adrian Nievergelt

MRC thumbnailing script

parent 6af5f535
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 15 09:22:40 2019
@author: adrian
"""
import configparser
import os, sys
from pprint import pprint
import re
import argparse
import mrcfile
import cv2
import numpy as np
parser = argparse.ArgumentParser(description = "Generates thumbnails for MRC microscope data.")
# parser.add_argument("-f", "--file", help="create overlay for a single file",
parser.add_argument('-i', '--input', nargs='?', default='.', help='Input MDOC file to process.')
parser.add_argument('-d', '--inDir', nargs='?', default='.', help='If specified, automatically process entire directory of mrc files.')
parser.add_argument('-mi', '--maxImages', nargs='?', type=float, default=30, help='If more images than specified are in a file, make amontage with a subset of the frames present instead of separate files. Detault is 30 images, minimum 6.')
# parser.add_argument('-g', '--graph', action='store_true', help='Store a graph of the corrected and uncorrected line intensities for debug purposes.')
args = parser.parse_args()
def FakeSecHead(fp): yield '[general]\n'; yield from fp
if args.input == '.':
print("Missing input file");
print(parser.print_help())
sys.exit()
if args.maxImages < 6:
print("Montage threshold needs to be at least 6.");
print(parser.print_help())
sys.exit()
parser = configparser.ConfigParser()
inFile = args.input.strip()
mrc = mrcfile.mmap(inFile)
inFileParts = os.path.splitext(inFile)
if len(mrc.data.shape) == 2:
zSlices = 1
else:
zSlices = mrc.data.shape[0]
makeMontage = zSlices > args.maxImages
if makeMontage:
xOutSize = 400;
montImages = []
print("Making a montage")
else:
xOutSize = 1000;
if makeMontage:
outputSlices = np.linspace(0, zSlices-1, 6).astype(int) # select 6 slices throughout the stack
else:
# likely stack of normal images, process them all into frames
outputSlices = range(zSlices)
outputFormat = "Writing output file %s."
for slice in range(zSlices):
if zSlices == 1:
image = mrc.data
else:
image = mrc.data[slice]
yOutSize = int(xOutSize/image.shape[0]*image.shape[1])
image = cv2.GaussianBlur(image, (9,9),4)
image = cv2.normalize(image, None, 0, 255,cv2.NORM_MINMAX)
image = cv2.resize(image, (xOutSize, yOutSize))
image = image.astype(np.uint8)
image = cv2.equalizeHist(image)
if makeMontage:
montImages.append(image)
else:
filePath = '%s_%i%s.png' % (inFileParts[0], slice+1, inFileParts[1])
print(outputFormat % (filePath))
cv2.imwrite(filePath, image)
if makeMontage:
fullMontage = np.vstack((np.hstack(montImages[0],montImages[1],montImages[2]),np.hstack(montImages[3],montImages[4],montImages[5])))
filePath = '%s_montage%s.png' % (inFileParts[0], slice+1, inFileParts[1])
print(outputFormat % (filePath))
cv2.imwrite(filePath, image)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment