[docs]@dataclassclassBox:""" Represents a rectangular region defined by its edges: left, top, right, and bottom. The class allows specifying these edges as absolute values or percentages of an image's dimensions. It includes methods to fill missing values and calculate the coordinates of the cut box based on an image's size. """left:int|float|None=Nonetop:int|float|None=Noneright:int|float|None=Nonebottom:int|float|None=Noneis_percents:InitVar[bool]=Falsedef__post_init__(self,is_percents:InitVar[bool]):self.is_percents=is_percents# is_percents will be ignored in `fields` and other dataclasses methods
[docs]deffill_values(self)->None:""" Replace :obj:`None` values for the cut box edges with `0`. This method ensures that all edges (left, top, right, bottom) have valid numerical values, defaulting to zero where no value is provided. :return: :obj:`None` """forname,valueinget_dict(self).items():setattr(self,name,valueor0)
[docs]defget_image_cut_box(self,size:Size)->Box:""" Calculate the cut box coordinates based on the provided image dimensions. This method computes the values of the left, top, right, and bottom edges, adjusting for whether the values are absolute or percentages of the image's size. :param size: :class:`.Size` object containing the width and height of the image size. :type size: Size :return: New :obj:`.Box` object representing the coordinates of the cut box (left, top, right, bottom). """width,height=size.width,size.heightself.fill_values()ifself.is_percents:left=self.left*width/100ifself.leftelseself.lefttop=self.top*height/100ifself.topelseself.topright=width-self.right*width/100ifself.rightelsewidthbottom=height-self.bottom*height/100ifself.bottomelseheightreturnBox(left=left,top=top,right=right,bottom=bottom)returnBox(left=self.left,top=self.top,right=width-self.right,bottom=height-self.bottom)