Commit 77cd6fa6 authored by Wallenfang, Nils's avatar Wallenfang, Nils

implement insertion of indices into preview image

parent 3b079d2f
......@@ -7,6 +7,8 @@ fiji_path = /home/nils/Public/Fiji.app/ImageJ-linux64
# or equal to (resolution_x, resolution_y)
resolution_x = 800
resolution_y = 1200
# unfortunately a path to a true type font has to be provided for insertion of index labels
font_path = /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
[GUI]
# initially selected tiff path, this directory is also used as a starting point when clicking 'browse'
......
from PIL import Image
import numpy as np
import pytiff
from os.path import isdir, join, isfile
......@@ -6,7 +5,10 @@ from os import listdir
import matplotlib.pyplot as plt
import cv2
from enum import Enum
from read_config import config
from utils.read_config import config
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
# TODO determine this by opening the first tiff
tile_x, tile_y = 2048, 2048
......@@ -37,7 +39,7 @@ def determine_scaling_factor(nx, ny, overlap, ):
def generate_preview(tiff_path, overlap=0.28, scaling=None, image_index=0, origin=IndexOrigin.BOTTOM_RIGHT,
on_tile_done=None):
on_tile_done=None, insert_index_labels=True):
"""
Returns a stitched image of the tiffs located in tiff_path.
......@@ -60,6 +62,8 @@ def generate_preview(tiff_path, overlap=0.28, scaling=None, image_index=0, origi
Callback that gets called upon insertion of a scaled tile into preview
image. X and y indices are passed as integer arguments. Defaults to
None.
insert_index_labels(bool, optional): If set to True insert a label containing the tile indices for each tile.
"""
directories = sorted([f for f in listdir(tiff_path) if isdir(join(tiff_path, f))])
last_dir = directories[-1]
......@@ -113,10 +117,38 @@ def generate_preview(tiff_path, overlap=0.28, scaling=None, image_index=0, origi
if on_tile_done is not None:
on_tile_done(x, y)
if insert_index_labels:
preview = insert_labels(preview, nx, ny)
return preview
def insert_labels(preview, xdim, ydim):
img = Image.fromarray(preview)
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(config['PREVIEWS']['font_path'], 12)
# tile dimensions
res_x = int(preview.shape[0] / xdim)
res_y = int(preview.shape[1] / ydim)
for x in range(xdim):
for y in range(ydim):
# take a small image sample to determine if font color
# should be black or white
avgColor = np.mean(preview[x * res_x:x * res_x + 20, y * res_y:y * res_y + 20])
print(avgColor, (0 if avgColor > 1000 else 255))
# draw.text((y * res_y, x * res_x), f"{x:02d}_{y:02d}", fill=(255 if avgColor > 1000 else 0))
draw.text((y * res_y, x * res_x), f"{xdim - x - 1:02d}_{ydim - y - 1git:02d}", (255 if avgColor > 1000 else 0))
return np.asarray(img)
if __name__ == '__main__':
def main():
preview = generate_preview('/data/PLI-Group/Nils/stitching_minimal/')
plt.imshow(preview, cmap='gray')
plt.show()
main()
......@@ -2,7 +2,7 @@ import tkinter
import logging
from tkinter import ttk
from read_config import config
from utils.read_config import config
class PreviewOptions(tkinter.LabelFrame):
......
......@@ -16,11 +16,12 @@ from wx.lib.agw.multidirdialog import (DD_DIR_MUST_EXIST, DD_MULTIPLE,
from analyze_section import analyze_section
from generate_preview import IndexOrigin, generate_preview
from utils.upload_log import upload_log
from gui_components import GUI
from qchecks.zero_check import ZeroCheck
from qchecks.sine_check import SineCheck
from qchecks.sine_measures import r2_measure
from read_config import config
from utils.read_config import config
# TODO add file with python dependencies, especially opencv and wxpython, maybe add error handling if these libraries
......@@ -57,6 +58,9 @@ def clicked_browse(frame):
def get_logger_filename(dir_name):
if not os.path.isdir(config['GUI']['log_path']):
os.mkdir(config['GUI']['log_path'])
file_name = f"{config['GUI']['log_path']}/{dir_name}.log"
if not os.path.isfile(file_name):
......@@ -150,7 +154,7 @@ def process_preview_gen(frame, tiff_path):
time_pre = time.time()
preview = generate_preview(tiff_path, overlap=overlap, origin=IndexOrigin.BOTTOM_RIGHT,
on_tile_done=progressbar_callback)
on_tile_done=progressbar_callback, insert_index_labels=True)
time_post = time.time()
......@@ -231,6 +235,9 @@ def process_quality_check(frame, tiff_path):
frame.print(f'Ran quality check in {time_post - time_pre:.2f}s')
# upload log file to status server
upload_log(tiff_path, logfile_path=None)
def check_directory(tiff_path, frame):
if not os.path.isdir(tiff_path):
......
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