Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To calculate a text item's bounding box ? #198

Open
wantsjean opened this issue Nov 1, 2022 · 2 comments
Open

How To calculate a text item's bounding box ? #198

wantsjean opened this issue Nov 1, 2022 · 2 comments

Comments

@wantsjean
Copy link

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

@loonghao
Copy link
Owner

loonghao commented Nov 5, 2022

@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)

@Investigamer
Copy link
Contributor

Investigamer commented Nov 7, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants