diff --git a/README.rst b/README.rst index 3fe19ab..f5d5f28 100644 --- a/README.rst +++ b/README.rst @@ -47,6 +47,7 @@ Usage [--addTextSize ] [--addTextPos ] [--addTextColor ] + [--addTextOffset ] [-h|--help] [--json] [--man] [--meta] [--savejson ] @@ -119,6 +120,10 @@ Arguments The color of the additional text to be shown on the image. Default is white. + [--addTextOffset ] + If specified, move the additional text using the offset + coordinates (x,y). Accepts a tuple in the form of "x,y" + [-h] [--help] If specified, show help message and exit. diff --git a/markimg/imageCanvas.py b/markimg/imageCanvas.py new file mode 100644 index 0000000..0855876 --- /dev/null +++ b/markimg/imageCanvas.py @@ -0,0 +1,78 @@ +""" +This class represents a canvas or X-Y plane of a certain height and width. +An object of this class offers the ability to move a point (x,y) on the canvas by using +simple direction commands (go_left, go_right, etc.). In addition to this, we can specify any +offset value to the current position +""" + + +class ImageCanvas: + def __init__(self, height: int, width: int): + self.height = height + self.width = width + self.current_X = 0 + self.current_Y = 0 + + def __get_left(self) -> (int, int): + """ + A method to return the mid-point on the left border of + the canvas + :return: x-y coordinates + """ + return 0, self.height / 2 + + def __get_right(self) -> (int, int): + """ + A method to return the mid-point of the right + border of the canvas + :return: x-y coordinates + """ + return self.width, self.height / 2 + + def __get_top(self) -> (int, int): + """ + A method to return the mid-point of the top + border of the canvas + :return: x-y coordinates + """ + return self.width / 2, self.height + + def __get_bottom(self) -> (int, int): + """ + A method to return the mid-point of the bottom + border of the canvas + :return: x-y coordinates + """ + return self.width / 2, 0 + + def __get_center(self) -> (int, int): + """ + A method to return the center of the X-Y plane + :return: x-y coordinates + """ + return self.width / 2, self.height / 2 + + def go_left(self) -> (int, int): + self.current_X, self.current_Y = self.__get_left() + return self.current_X, self.current_Y + + def go_right(self) -> (int, int): + self.current_X, self.current_Y = self.__get_right() + return self.current_X, self.current_Y + + def go_top(self) -> (int, int): + self.current_X, self.current_Y = self.__get_top() + return self.current_X, self.current_Y + + def go_bottom(self) -> (int, int): + self.current_X, self.current_Y = self.__get_bottom() + return self.current_X, self.current_Y + + def go_center(self) -> (int, int): + self.current_X, self.current_Y = self.__get_center() + return self.current_X, self.current_Y + + def add_offset(self, offset_x: int, offset_y: int) -> (int, int): + self.current_X += offset_x + self.current_Y += offset_y + return self.current_X, self.current_Y diff --git a/markimg/markimg.py b/markimg/markimg.py index 9ff101f..b347191 100644 --- a/markimg/markimg.py +++ b/markimg/markimg.py @@ -8,23 +8,18 @@ # dev@babyMRI.org # -from chrisapp.base import ChrisApp -import matplotlib.pyplot as plt -import numpy as np +import glob import json import math -import os, sys -import glob +import os +import sys + import cv2 -import matplotlib.legend as lgnd -import time +import matplotlib.pyplot as plt +from chrisapp.base import ChrisApp from loguru import logger - -from pftag import pftag from pflog import pflog - -from argparse import Namespace -from datetime import datetime +from markimg.imageCanvas import ImageCanvas LOG = logger.debug @@ -76,6 +71,7 @@ [--addTextSize ] \\ [--addTextPos ] \\ [--addTextColor ] \\ + [--addTextOffset ] \\ [-h] [--help] \\ [--json] \\ [--man] \\ @@ -180,6 +176,11 @@ [--addTextColor ] The color of the additional text to be shown on the image. Default is white. + + [--addTextOffset ] + If specified, move the additional text using the offset + coordinates (x,y). Accepts a tuple in the form of "x,y" + [-h] [--help] If specified, show help message and exit. @@ -348,6 +349,13 @@ def define_parameters(self): optional=True, help='Color of additional text on the final output,' 'default value is white') + self.add_argument('--addTextOffset', + dest='addTextOffset', + default='0,0', + type=str, + optional=True, + help='Offset of additional text on the final output,' + 'default value is 0,0') def preamble_show(self, options) -> None: """ @@ -409,6 +417,7 @@ def run(self, options): plt.axis('off') max_y, max_x, max_z = image.shape + img_XY_plane: ImageCanvas = ImageCanvas(max_y, max_x) height = data[row]["origHeight"] ht_scale = height / max_x @@ -538,30 +547,37 @@ def run(self, options): x_pos = x_pos + line_gap plt.text(x_pos, y_pos, '', color='white', fontsize=options.textSize, rotation=90) rotation = 0 - x_pos = x_pos + line_gap - if options.addTextPos == "top": - x_pos = 0 - y_pos = max_y - rotation = 90 + + #x_pos = x_pos + line_gap + """ + Need to rewrite login for directions. + """ + if options.addTextPos == "left": + x_pos, y_pos = img_XY_plane.go_top() + elif options.addTextPos == "right": + x_pos, y_pos = img_XY_plane.go_bottom() elif options.addTextPos == "bottom": - x_pos = max_x - y_pos = max_y + x_pos, y_pos = img_XY_plane.go_right() + rotation = 90 + elif options.addTextPos == "top": + x_pos, y_pos = img_XY_plane.go_left() rotation = 90 - elif options.addTextPos == "right": - x_pos = x_pos - y_pos = 0 - elif options.addTextPos == "left": - x_pos = x_pos - y_pos = max_y elif options.addTextPos == "across": - x_pos = x_pos - y_pos = max_y - rotation = 90 # 135: diagonal [bottom-left - top-right] + x_pos, y_pos = img_XY_plane.go_center() + rotation = 90 # 135: diagonal [bottom-left - top-right] else: raise Exception(f"Incorrect line position specified: {options.linePos}") - y_pos = y_pos - line_gap - plt.text(x_pos, y_pos, options.addText, color=options.addTextColor, fontsize=options.addTextSize, rotation=rotation) + if len(options.addTextOffset): + offset = options.addTextOffset.split(',') + offset_y = int(offset[0]) + offset_x = int(offset[1]) + x_pos, y_pos = img_XY_plane.add_offset(-offset_x, -offset_y) + + # y_pos = y_pos - line_gap + + plt.text(x_pos, y_pos, options.addText, color=options.addTextColor, fontsize=options.addTextSize, + rotation=rotation) # Clean up all matplotlib stuff and save as PNG plt.tick_params(left=False, right=False, labelleft=False, diff --git a/setup.py b/setup.py index 9775033..afb8bff 100755 --- a/setup.py +++ b/setup.py @@ -1,27 +1,28 @@ from os import path + from setuptools import setup with open(path.join(path.dirname(path.abspath(__file__)), 'README.rst')) as f: readme = f.read() setup( - name = 'markimg', - version = '1.3.2', - description = 'An app to mark landmark points and lines on an input image', - long_description = readme, - author = 'FNNDSC', - author_email = 'dev@babyMRI.org', - url = 'http://wiki', - packages = ['markimg'], - install_requires = ['chrisapp'], - test_suite = 'nose.collector', - tests_require = ['nose'], - license = 'MIT', - zip_safe = False, - python_requires = '>=3.6', - entry_points = { + name='markimg', + version='1.3.2', + description='An app to mark landmark points and lines on an input image', + long_description=readme, + author='FNNDSC', + author_email='dev@babyMRI.org', + url='http://wiki', + packages=['markimg'], + install_requires=['chrisapp'], + test_suite='nose.collector', + tests_require=['nose'], + license='MIT', + zip_safe=False, + python_requires='>=3.6', + entry_points={ 'console_scripts': [ 'markimg = markimg.__main__:main' - ] - } + ] + } )