Permalink
Cannot retrieve contributors at this time
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?
hyperimage_scripts/layercalc.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (52 sloc)
2.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |