photoshop-python-api icon indicating copy to clipboard operation
photoshop-python-api copied to clipboard

How To calculate a text item's bounding box ?

Open wantsjean opened this issue 3 years ago • 2 comments
trafficstars

Is there a way to calculate a text's real bounding box?

wantsjean avatar Nov 01 '22 09:11 wantsjean

@wantsjean you can try below codes.

from photoshop import Session

with Session() as ps:
    doc = ps.active_document
    layer = doc.activeLayer
    print(layer.textItem.size)
    print(layer.bounds)

loonghao avatar Nov 05 '22 13:11 loonghao

@wantsjean If you're trying to get the dimensions of the bounding box of the text item, you can do:

height, width = layer.textItem.height, layer.textItem.width

This is for the bounding box, but if you want the dimensions of JUST the text without the spacing of the bounding box:

def get_layer_dimensions(layer):
    """
    Compute the width and height dimensions of a layer.
    @param layer: A layer object
    @return dict: Height and width of the layer.
    """
    return {
        'width': layer.bounds[2]-layer.bounds[0],
        'height': layer.bounds[3]-layer.bounds[1],
    }

If you're talking about the vertices of the bounding box on a grid, use hal's answer, you can use layer.bounds to construct coordinates

Investigamer avatar Nov 07 '22 20:11 Investigamer