Commit e7dcd0ce authored by Adrian Nievergelt's avatar Adrian Nievergelt

directroy mode for thumbnailer

parent 7d3ecfef
......@@ -6,10 +6,7 @@ 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
......@@ -21,6 +18,7 @@ parser = argparse.ArgumentParser(description = "Generates thumbnails for MRC mic
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('-sd', '--subDirs', action='store_true', help='Include subdirectories when in directory mode.')
# 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()
......@@ -28,71 +26,85 @@ 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()
dirMode = True
inFileList = [args.input.strip()]
# print("Missing input file");
# print(parser.print_help())
# sys.exit()
else:
dirMode = False
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 outputSlices:
if zSlices == 1:
image = mrc.data
if dirMode and args.inDir is not None and os.path.isdir(args.inDir):
inFileList = []
for (dirpath, dirnames, filenames) in os.walk(args.inDir):
filenames.sort()
newFileList = [os.path.join(dirpath, fName) for fName in filenames if fName.endswith('.mrc')]
inFileList.extend(newFileList)
if not args.subDirs:
break
for inFile in inFileList:
try:
mrc = mrcfile.mmap(inFile)
except:
print("Failed to open input file %s! Skipping!" % (inFile))
inFileParts = os.path.splitext(inFile)
if len(mrc.data.shape) == 2:
zSlices = 1
else:
image = mrc.data[slice]
zSlices = mrc.data.shape[0]
yOutSize = int(xOutSize/image.shape[0]*image.shape[1])
makeMontage = zSlices > args.maxImages
# flip horizontally
image = cv2.flip(image, 0)
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)
xOutSize = 400;
montImages = []
# print("Making a montage")
else:
filePath = '%s_%i%s.png' % (inFileParts[0], slice+1, inFileParts[1])
print(outputFormat % (filePath))
cv2.imwrite(filePath, image)
if makeMontage:
xOutSize = 1000;
fullMontage = np.vstack((np.hstack((montImages[0],montImages[1],montImages[2])),np.hstack((montImages[3],montImages[4],montImages[5]))))
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 outputSlices:
if zSlices == 1:
image = mrc.data
else:
image = mrc.data[slice]
yOutSize = int(xOutSize/image.shape[0]*image.shape[1])
# flip horizontally
image = cv2.flip(image, 0)
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)
filePath = '%s_montage%s.png' % (inFileParts[0], inFileParts[1])
print(outputFormat % (filePath))
cv2.imwrite(filePath, fullMontage)
\ No newline at end of file
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], inFileParts[1])
print(outputFormat % (filePath))
cv2.imwrite(filePath, fullMontage)
\ 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