Skip to content

Commit

Permalink
Added new python module to handle position of additional text
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandip117 committed Sep 28, 2023
1 parent 3b5b5e9 commit 2106a4b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 47 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Usage
[--addTextSize <additionalTextSize>]
[--addTextPos <additionalTextPosition>]
[--addTextColor <additionalTextColor>]
[--addTextOffset <additionalTextOffsetPosition>]
[-h|--help]
[--json] [--man] [--meta]
[--savejson <DIR>]
Expand Down Expand Up @@ -119,6 +120,10 @@ Arguments
The color of the additional text to be shown on the image.
Default is white.
[--addTextOffset <additionalTextOffset>]
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.
Expand Down
78 changes: 78 additions & 0 deletions markimg/imageCanvas.py
Original file line number Diff line number Diff line change
@@ -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
76 changes: 46 additions & 30 deletions markimg/markimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@
# [email protected]
#

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

Expand Down Expand Up @@ -76,6 +71,7 @@
[--addTextSize <additionalTextSize>] \\
[--addTextPos <additionalTextPosition>] \\
[--addTextColor <additionalTextColor>] \\
[--addTextOffset <additionalTextOffsetPosition>] \\
[-h] [--help] \\
[--json] \\
[--man] \\
Expand Down Expand Up @@ -180,6 +176,11 @@
[--addTextColor <additionalTextColor>]
The color of the additional text to be shown on the image.
Default is white.
[--addTextOffset <additionalTextOffset>]
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.
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
35 changes: 18 additions & 17 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]',
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='[email protected]',
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'
]
}
]
}
)

0 comments on commit 2106a4b

Please sign in to comment.