imagemagickimagemagick-convertimagemagick-identify

Trim image with transparency around and know it's location


I know it is possible to trim all transparency around image to make image smaller and contain only the image. But is it also possible to somehow know the location of "box", that contains the image? For example I have 100x100 transparent image, which contains something at 10x10 box having topleft corner at x=15,y=15. Rest is all transparent.

I'd like to end up with 10x10 image, all transparency around trimmed, but also having that 15,15 information. Those are probably 2 separate actions. How do I do this in a script?

Just fyi - I am having bunch of images like this and they are layers, that I need to trim and stack onto eachother to make them clickable.


Solution

  • There are lots and lots of words but no image in your question so I am trying to guess what you want. I made this input image:

    magick -size 100x100 xc:black -fill white -draw "rectangle 10,20 50,80" image.png
    

    enter image description here

    And I think you want to know the trim box, which is where it would trim to if you ran -trim:

    magick image.png -format "%@" info:
    41x61+10+20
    

    So that's a 41x61 box with the top-left at (10,20).