import numpy as np
import tifffile as tif
from PIL import Image
import glob
import matplotlib as plt
import scipy as sp
import os
import re
class PreProcess:
def __init__(self, file, save_location):
'''
Inputs:
file (str): file path of one image from the group of images
that you want to process. For this program to work
as expected, all files should have the same path aside from
a numerical designation at the end of their path (i.e.
"some/random/path/img_1.TIF" and all images have the same file
path structure "some/random/path/img_2.TIF")
save_location (str): path to a location where you would like
to save the converted TIFs and the final .npz image stack
'''
self.file = file
self.save_location = save_location
def tif_to_png(self):
'''
Converts all TIF images in a given location to .png images.
Images are saved in the same location as the original images.
'''
self.file = self.file[:-4] + "*.TIF"
files = glob.glob(self.file)
total_images = len(files)
for i, path in enumerate(files):
im = np.array(Image.open(path))
im = Image.fromarray(im / np.amax(im) * 255)
im = im.convert("L")
path = str(path).rstrip(".TIF")
im.save(path + '.png', 'PNG')
print("Image {} of {} Saved".format(i, total_images))
def crop_vignette(self, check_size = False, crop_dim = (289.5, 129.5, 1054.5, 894.5),
show_img = False, save_img = True):
'''
Removes black vignetting from the images.
Inputs:
check_size (bool): default is False. Will print the dimensions of
the input image if True.
crop_dim (tup): default is (289.5, 129.5, 1054.5, 894.5). This may need
to be adjusted depending on the input image dimensions. Note, the
upper left corner is the origin (0,0)
show_img (bool): default is False. Will show the cropped image
if True.
save_img (bool): default is True. Will not save the image if set to False.
This is useful when working with many images, as you may
want to check that the images are being appropriately cropped and
adjust dimensions accordingly before saving all images.
'''
self.file = self.file[:-4] + "*.png"
files = glob.glob(self.file)
total_images = len(files)
for i, f in enumerate(files):
img = Image.open(f)
if check_size:
w, h = img.size
print("Width = {}, Height = {}".format(w, h))
img = img.crop(crop_dim)
if show_img:
img.show()
if save_img:
name = str(f)
image_name = re.split(r"/", name)[-1]
path = self.save_location + image_name
img.save(path)
print("Image {} of {} Cropped".format(i, total_images))
def png_to_npz(self, height = 256, width = 256, input_dim = 512):
'''
Convert .png images into a three channel .npz stack.
Inputs:
height (int): desired output image height
width (int): desired output image width
input_dim (int): default = 512. Input image height to
add a third channel to the two-channel black and white image.
The third channel is needed for .npz compatability with DeepCell.
May need to adjust this if the dimensions were changed from the default
paramenters in crop_vignette().
'''
img_array = []
self.file = self.file[:-4] + "*.png"
files = glob.glob(self.file)
channels = np.zeros((input_dim,1))
for f in files:
img = Image.open(f)
img = img.resize((height, width))
np_img = np.asarray(img)
img_array.append(np_img)
im_stack = np.array(img_array)
im_stack = np.expand_dims(im_stack, axis = 3)
np.savez_compressed(self.save_location)