You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.8 KiB
95 lines
2.8 KiB
from PIL import Image |
|
|
|
import numpy |
|
|
|
from .commons import ( |
|
TifArray, |
|
IMG_MAX_INTENSITY, |
|
FIRE_LUT, |
|
ROI_STATISTIC_FUNCTIONS, |
|
) |
|
|
|
|
|
def open_as_array(tif_image): |
|
with tif_image.path.open("rb") as filehandle: |
|
image = Image.open(filehandle) |
|
image_array = numpy.array(image, dtype=numpy.int32) |
|
return TifArray( |
|
path=tif_image.path, frame=tif_image.frame, data=image_array |
|
) |
|
|
|
|
|
def scale_and_colorize(tif_array, parameters): |
|
adjusted_array = (tif_array.data - parameters.offset) // parameters.scale |
|
|
|
# paint roi |
|
top = parameters.roi_top |
|
bottom = parameters.roi_bottom |
|
right = parameters.roi_right |
|
left = parameters.roi_left |
|
adjusted_array[top, left:right] = IMG_MAX_INTENSITY |
|
adjusted_array[bottom, left:right] = IMG_MAX_INTENSITY |
|
adjusted_array[top:bottom, left] = IMG_MAX_INTENSITY |
|
adjusted_array[top:bottom, right] = IMG_MAX_INTENSITY |
|
|
|
adjusted = Image.fromarray(adjusted_array) |
|
converted = adjusted.convert(mode="L").convert(mode="P") |
|
converted.putpalette(FIRE_LUT) |
|
return converted.convert("RGB") |
|
|
|
|
|
def safe_color_coded(tif_array, parameters): |
|
png_image = scale_and_colorize(tif_array, parameters) |
|
png_path = parameters.colored_dir / (tif_array.path.stem + ".jpg") |
|
with png_path.open("wb") as outhandle: |
|
png_image.save(outhandle) |
|
del png_image |
|
|
|
|
|
def safe_cut(tif_array, parameters): |
|
cut_path = parameters.cuts_dir / tif_array.path.name |
|
|
|
top = parameters.cut_top |
|
bottom = parameters.cut_bottom |
|
right = parameters.cut_right |
|
left = parameters.cut_left |
|
|
|
with cut_path.open("wb") as outhandle: |
|
cut_array = tif_array.data[top:bottom, left:right] |
|
cut_img = Image.fromarray(cut_array) |
|
cut_img.save(outhandle) |
|
del cut_img |
|
|
|
|
|
def get_roi(tif_array, parameters): |
|
top = parameters.roi_top |
|
bottom = parameters.roi_bottom |
|
right = parameters.roi_right |
|
left = parameters.roi_left |
|
return tif_array.data[top:bottom, left:right] |
|
|
|
|
|
def analyse_roi(tif_array, parameters): |
|
data = get_roi(tif_array, parameters) |
|
results = {} |
|
results.update(get_stats(data[:, 1], parameters.left_guard_name)) |
|
results.update(get_stats(data[:, 1:-1], parameters.roi_name)) |
|
results.update(get_stats(data[:, -1], parameters.right_guard_name)) |
|
return results |
|
|
|
|
|
def get_stats(array, prefix): |
|
return { |
|
f"{prefix}.{name}": func(array) |
|
for name, func in ROI_STATISTIC_FUNCTIONS.items() |
|
} |
|
|
|
|
|
def process_one_tif(tif_image, parameters): |
|
tif_array = open_as_array(tif_image) |
|
safe_color_coded(tif_array, parameters) |
|
safe_cut(tif_array, parameters) |
|
stats_result = analyse_roi(tif_array, parameters) |
|
stats_result["file"] = tif_image.path.name |
|
stats_result["frame"] = tif_image.frame |
|
return stats_result
|
|
|