Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env python3
# -*- coding: utf-8; mode: python -*-
"""
Return relative coordinates of a rectangle for insertion in hyperimage.
"""
__version__ = "1.0"
__date__ = "20191218"
__author__ = "kthoden@mpiwg-berlin.mpg.de"
import argparse
import logging
from PIL import Image
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
def get_image_size(imagefile):
"""Get image size of image. Return tuple of width and height"""
image_object = Image.open(imagefile)
width, height = image_object.size
return width, height
# def get_image_size ends here
def calculate_coordinate(relative, absolute):
"""Calculate relative coordinate"""
fraction = relative / absolute
return fraction
# def calculate_coordinate ends here
def get_relative_coordinates(width, height, x0, y0, x1, y1):
"""Assemble the list of relative coordinates."""
list_of_points = []
topleft = [calculate_coordinate(x0, width), calculate_coordinate(y0, height)]
topright = [calculate_coordinate(x1, width), calculate_coordinate(y0, height)]
bottomleft = [calculate_coordinate(x1, width), calculate_coordinate(y1, height)]
bottomright = [calculate_coordinate(x0, width), calculate_coordinate(y1, height)]
list_of_points.append(topleft)
list_of_points.append(topright)
list_of_points.append(bottomleft)
list_of_points.append(bottomright)
return list_of_points
# def get_relative_coordinates ends here
def main():
"""The main bit"""
parser = argparse.ArgumentParser()
parser.add_argument("image", help="The image on which the layer is supposed to sit.")
parser.add_argument("topleft", help="Absolute coordinates of top left corner, insert as x,y")
parser.add_argument("bottomright", help="Absolute coordinates of bottom right corner, insert as x,y")
args = parser.parse_args()
width, height = get_image_size(args.image)
topleft_width, topleft_height = args.topleft.split(",")
bottomright_width, bottomright_height = args.bottomright.split(",")
points = get_relative_coordinates(int(width), int(height), int(topleft_width), int(topleft_height), int(bottomright_width), int(bottomright_height))
print(points)
# def main ends here
if __name__ == '__main__':
main()
# finis